Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.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
Asp.net core ASP.NET MVC核心中的优先约定与基于属性的路由_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

Asp.net core ASP.NET MVC核心中的优先约定与基于属性的路由

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

如果我在ASP.NET MVC核心应用程序中既有基于约定的路由,也有基于属性的路由。如果两者匹配,哪种类型的路由优先?

简而言之:基于属性的路由优先

例如:

因为你没有提供任何我能最好解释的例子

这里是我在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));

简而言之:基于属性的路由优先

例如:

因为你没有提供任何我能最好解释的例子

这里是我在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));