简体   繁体   English

如何在 ASP.NET Core 3.1 MVC 中进行自定义路由

[英]How to do custom routing in ASP.NET Core 3.1 MVC

I can't use simple routing like in .NET Core 2.2 in .NET Core 3.1.我不能在 .NET Core 3.1 中使用像在 .NET Core 2.2 中那样的简单路由。

What is the routing last change in .NET Core 3.1? .NET Core 3.1 中路由的最后更改是什么?

In .NET 3 you should use Endpoint instead of Routing在 .NET 3 中你应该使用Endpoint而不是Routing

app.UseStaticFiles();
app.UseRouting();
//other middleware

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
    endpoints.MapRazorPages();
    endpoints.MapHub<MyChatHub>();
    endpoints.MapGrpcService<MyCalculatorService>();
    endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
});

Next to Endpoint's you can also use attribute routing, or combine the two.在端点旁边,您还可以使用属性路由,或将两者结合起来。

[Route("my/")]
public class MyController : Controller

[HttpGet]
[AllowAnonymous]
[Route("")] //prefer this if we asked for this action
[Route("index", Order = 1)]
[Route("default.aspx", Order = 100)] // legacy might as well get an order of 100
public async Task<IActionResult> GetIndex()
{
}

With the above attribute for the controller, you do not need to specify MapControllerRoute for this controller.使用控制器的上述属性,您不需要为此控制器指定 MapControllerRoute。 The action has three routes in this example.在此示例中,该操作具有三个路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 ASP.NET Core 3.1 中的参数进行路由 - How to do routing with parameters in ASP.NET Core 3.1 ASP.NET 核心 3.1 路由 - API 和 MVC 路由混淆 - ASP.NET Core 3.1 Routing - API and MVC Route confusion ASP.NET CORE 3.1 为客户定制 Razor 页面路由 - ASP.NET CORE 3.1 Custom Razor Page Routing for Customers Asp.net core 3.1 中的路由 - Routing in Asp.net core 3.1 ASP.NET Core 3.1 路由(Route, ActionName)本地化怎么做? - How to do ASP.NET Core 3.1 routing (Route, ActionName) localization? 如何使用 ASP.NET Core 3.1 MVC 和 EF Core 对唯一字段使用数据验证注释? - How do use data validation annotation for unique fields with ASP.NET Core 3.1 MVC and EF Core? 如何在 Asp.Net Core MVC 中进行自定义 model 绑定 - How to do custom model binding in Asp.Net Core MVC 如何在 ASP.NET Core 3.1 MVC 中设置 wkhtmltopdf 选项? - How to set wkhtmltopdf options in ASP.NET Core 3.1 MVC? 如何在 asp.net 核心 3.1 mvc web 应用程序中将日志保存到数据库? - How do I save logs to database in asp.net core 3.1 mvc web application? 从 MVC 迁移到 ASP.NET Core 3.1 中的端点路由时,具有角色的 AuthorizeAttribute 不起作用 - AuthorizeAttribute with Roles not working when migrating from MVC to Endpoint Routing in ASP.NET Core 3.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM