Asp.net core ASP.NET MVC核心中的优先约定与基于属性的路由
如果我在ASP.NET MVC核心应用程序中既有基于约定的路由,也有基于属性的路由。如果两者匹配,哪种类型的路由优先?简而言之:基于属性的路由优先 例如: 因为你没有提供任何我能最好解释的例子 这里是我在app.UseMvc中配置的Asp.net core ASP.NET MVC核心中的优先约定与基于属性的路由,asp.net-core,asp.net-core-mvc,Asp.net Core,Asp.net Core Mvc,如果我在ASP.NET MVC核心应用程序中既有基于约定的路由,也有基于属性的路由。如果两者匹配,哪种类型的路由优先?简而言之:基于属性的路由优先 例如: 因为你没有提供任何我能最好解释的例子 这里是我在app.UseMvc中配置的 app.UseMvc(routes => { routes.MapRoute( name: "default1", template: "{contro
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default1",
template: "{controller=My}/{action=Index}/{id:int}"); // This route I added for your question clarification.
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
控制器看起来像这样
[Route("My")] // Disable this line if attribute route need to be disable.
public class MyController : Controller
{
[Route("Index/{id:min(20)}")] // Disable this line if attribute route need to be disable.
// GET: /<controller>/
public IActionResult Index(int id)
{
return Content("This is just test : " + id.ToString());
}
}
现在您知道为什么首先添加基于属性的路由,然后再添加其他路由
在Github asp.net核心源代码中,请参阅以下文件
在UseMvc的一个扩展方法中,您将看到以下行
routes.Routes.Insert(0, AttributeRouting.CreateAttributeMegaRoute(app.ApplicationServices));
简而言之:基于属性的路由优先 例如: 因为你没有提供任何我能最好解释的例子 这里是我在app.UseMvc中配置的
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default1",
template: "{controller=My}/{action=Index}/{id:int}"); // This route I added for your question clarification.
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
控制器看起来像这样
[Route("My")] // Disable this line if attribute route need to be disable.
public class MyController : Controller
{
[Route("Index/{id:min(20)}")] // Disable this line if attribute route need to be disable.
// GET: /<controller>/
public IActionResult Index(int id)
{
return Content("This is just test : " + id.ToString());
}
}
现在您知道为什么首先添加基于属性的路由,然后再添加其他路由
在Github asp.net核心源代码中,请参阅以下文件
在UseMvc的一个扩展方法中,您将看到以下行
routes.Routes.Insert(0, AttributeRouting.CreateAttributeMegaRoute(app.ApplicationServices));