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" } );