Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NET Core 3.1 MVC中进行自定义路由_C#_.net_Asp.net Core_.net Core 3.0_Asp.net Core 3.1 - Fatal编程技术网

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

C# 如何在ASP.NET Core 3.1 MVC中进行自定义路由,c#,.net,asp.net-core,.net-core-3.0,asp.net-core-3.1,C#,.net,Asp.net Core,.net Core 3.0,Asp.net Core 3.1,我不能像.NETCore2.2和.NETCore3.1那样使用简单的路由 .NET Core 3.1中的最后一次路由更改是什么?在.NET 3中,您应该使用端点而不是路由 app.UseStaticFiles(); app.UseRouting(); //other middleware app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapRazorPages(); en

我不能像.NETCore2.2和.NETCore3.1那样使用简单的路由


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

在.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", pattern: "{controller=Home}/{action=Index}/{id?}");
});
app.UseStaticFiles();
app.UseRouting();
//其他中间件
app.UseEndpoints(端点=>
{
endpoints.MapControllers();
endpoints.MapRazorPages();
endpoints.MapHub();
endpoints.MapGrpcService();
MapControllerRoute(名称:“默认”,模式:“{controller=Home}/{action=Index}/{id?}”);
});

在端点旁边,您还可以使用属性路由,或将两者结合使用

[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()
{
}
[路线(“我的/”)]
公共类MyController:Controller
[HttpGet]
[异名]
[Route(“”)//如果我们要求执行此操作,则更喜欢此选项
[路线(“索引”,顺序=1)]
[Route(“default.aspx”,Order=100)]//legacy也可以获得100的订单
公共异步任务GetIndex()
{
}

使用控制器的上述属性,不需要为此控制器指定MapControllerRoute。在本例中,该操作有三条路由

如中所述,更改从2.2更改为3.0。对于MVC,可以通过设置
services.AddMvc(options=>options.EnableEndpointRouting=false)来创建新的端点路由