zoukankan      html  css  js  c++  java
  • 路由匹配规则

      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  “规则有规定,但URL没传,有默认值时,可以匹配该条路由”

    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 “路由规定没有,而URL有传ID时,需匹配带ID的路由”  "/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 }
                );
    

    5默认值是否配置

     默认值有配置,"/students/Details.aspx" 会被截获

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

    默认值无配置时,,"/students/Details.aspx" 不被截获,缺少ID并不匹配该路由

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

      

  • 相关阅读:
    喜大普奔,微软Microsoft JDBC Driver For SQL Server已发布到maven中央仓库
    maven jdbc 驱动安装
    SpringIoC和SpringMVC的快速入门
    再见 Spring Boot 1.X ,Spring Boot 2.X 走向舞台中心
    LRU设计
    二叉搜索树转换成双向链表
    快速排序
    二叉搜索树的先序中序后序非递归遍历代码
    EM算法
    模型调优
  • 原文地址:https://www.cnblogs.com/tianxiaoxiao/p/7923070.html
Copyright © 2011-2022 走看看