路由匹配规则

Posted C小小

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了路由匹配规则相关的知识,希望对你有一定的参考价值。

  routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",  //相当  kong + {controller} +{controller}/{action}+{controller}/{action}/{id}
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

  1所以有以上的路由规则,一定要写在Defalut之前,否则会被拦截,

  2路由不会先检测常量,只会由上至下,依次匹配,匹配到了,再对比规则,然后再匹配规则(常量的严格匹配),

  所以"/students/Details.aspx"会先被【2】截获,然后常量又不匹配,进而报错

          ////【2】测试{name}-{age}
            routes.MapRoute(
                name: "Test4",
                url: "{name}-{age}",
                defaults: new { controller = "Work", action = "Index", id = UrlParameter.Optional }
            );

            //【5】测试{table}/Details.apx
            routes.MapRoute(
                name: "Test8",
                url: "{table}/Details.aspx",
               defaults: new { controller = "Work", action = "Index" }
            );

 3 controller action id都有默认值,所以href="/blogs/tom/news" 匹配的是规则【2】,规则有,而URL无的时候,取默认值,被2截获

    //【2】测试字面量和常量必须严格匹配
            routes.MapRoute(
                name: "Test2",
                url: "blogs/{author}/{category}/{id}",
                defaults: new { controller = "Work", action = "Index2", id = UrlParameter.Optional }
            );

            //【3】测试字面量和常量必须严格匹配
            routes.MapRoute(
                name: "Test5",
                url: "blogs/{author}/{category}",
                defaults: new { controller = "Work", action = "Index5", id = UrlParameter.Optional }
            );

4   "/blogs/tom/news/1"  匹配的是【3】规则无,而rul有的话,继续找。

 //【2】测试字面量和常量必须严格匹配
            routes.MapRoute(
                name: "Test5",
                url: "blogs/{author}/{category}",
                defaults: new { controller = "Work", action = "Index5", id = UrlParameter.Optional }
            );

            //【3】测试字面量和常量必须严格匹配
            routes.MapRoute(
                name: "Test2",
                url: "blogs/{author}/{category}/{id}",
                defaults: new { controller = "Work", action = "Index2", id = UrlParameter.Optional }
            );

  

以上是关于路由匹配规则的主要内容,如果未能解决你的问题,请参考以下文章

正则匹配路由

Vue路由匹配规则routes中的name属性

djanjo路由匹配规则

gateway网关路由匹配规则

路由匹配规则

路由规则urls