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

我不能像.NET Core 3.1中的.NET Core 2.2中那样使用简单的路由。

.NET Core 3.1中的路由最后更改是什么?

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

在.NET 3中,您应该使用端点而不是路由

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

 app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapRazorPages();
                endpoints.MapHub<MyChatHub>();
                endpoints.MapGrpcService<MyCalculatorService>();

                endpoints.MapControllerRoute(name: "default"{controller=Home}/{action=Index}/{id?}");
            });

,

在Endpoint旁边,您还可以使用属性路由,或将两者结合在一起。

[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()
{
}

使用控制器的以上属性,您无需为此控制器指定MapControllerRoute。在此示例中,动作有3条路线。

本文链接:https://www.f2er.com/2958742.html

大家都在问