ASP.Net MVC routing and routing debugging tools RouteDebug

First, routing rules

  1, you can create multiple routes to the rules, each route name attribute is not the same

  2, the routing rules have priority, the higher the top priority routing rules

RouteConfig.cs: file under App_Start

 1         public static void RegisterRoutes(RouteCollection routes)
 2         {
 3             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 4 
 5             routes.MapRoute(
 6                 name: "Default2",
 7                 url: "{controller}-{action}",
 8                 defaults: new { controller = "HomeDemo", action = "Index" }
 9             );
10 
11             routes.MapRoute(
12                 name: "Default",
13                 url: "{controller}/{action}/{id}",
14                 defaults: new { controller = "HomeDemo", action = "Index", id = UrlParameter.Optional }
15             );
16         }
17     }

Second, routing debugging tools

  When registering multiple routes for our application, due to the improper registration, you can not get the desired results. Why does this happen, specific requests which go route? This time the hero RegisterRoutes turn.

The first step: first download dll library, I did not go to the Baidu cloud disk download

Link: https: //pan.baidu.com/s/1jJ1W88cOuTrdooLySnGVSg
extraction code: 097u
second step: the introduction of package: RouteDebug

Third step: to override the test path Global.asax

 1     public class MvcApplication : System.Web.HttpApplication
 2     {
 3         protected void Application_Start()
 4         {
 5             AreaRegistration.RegisterAllAreas();
 6             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
 7             RouteConfig.RegisterRoutes(RouteTable.Routes);
 8             BundleConfig.RegisterBundles(BundleTable.Bundles);
 9 
10             RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes); //重写测试路径
11         }
12     }

第四步:运行网站,进行分析路由规则

 

搞定!~~

 三、路由的约束

 1     public class RouteConfig
 2     {
 3         public static void RegisterRoutes(RouteCollection routes)
 4         {
 5             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 6 
 7             routes.MapRoute(
 8                 name: "Default2",
 9                 url: "{controller}-{action}",
10                 defaults: new { controller = "HomeDemo", action = "Index" },
11                 constraints:new {Controller=@"^\d+$" }, //控制器约束
12                 namespaces:new string[] { "MVCDemo2.Controllers" } //命名空间约束
13             );
14 
15             routes.MapRoute(
16                 name: "Default",
17                 url: "{controller}/{action}/{id}",
18                 defaults: new { controller = "HomeDemo", action = "Index", id = UrlParameter.Optional }
19             );
20         }
21     }

 

Guess you like

Origin www.cnblogs.com/chenyanbin/p/11279902.html