[英]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.