【基础】MVC路由规则
RouteData解析过程
在ASP.NET MVC中,服务器收到来自客户端的请求后,会经过一些列的处理拿到请求的数据,比如在Pipeline 管线事件中,通过订阅适当的事件,将HttpContext
作为参数传入HttpContextWrapper
进行封装,然后取得当前路由集合的数据RouteData
进行解析,拿到具体的参数,包括请求路径、请求的参数、IRouteHandler
等,通过IRouteHandler
的GetHttpHandler
返回一个IHttpHandler
对象,通过该对象的ProcessRequest
对请求进行处理,然后控制器工厂通过RouteData
中匹配的Controller进行反射构造一个Controller
,Controller
调用IController
的Excute
方法,同样是通过反射拿到当前请求的Action
,最后执行Action
,返回客户端数据,完成本次的请求。整体流程图如下所示:
在这个过程中,RouteData
中的路由 起到了很大的作用。Routing的作用:首先通过HTTP请求,并解析Url请求中Controller
和Action
以及附加数据,其次将识别出来的数据传递给Controller
的Action
(Controller的方法)。这是Routing组件的两个重要的作用!
路由规则实例解析
实例一:系统默认提供的路由格式
下面是系统给的默认代码:
1 public static void RegisterRoutes(RouteCollection routes) 2 { 3 routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 4 5 routes.MapRoute( 6 "Default", // 路由名称 7 "{controller}/{action}/{id}", // 带有参数的 URL 8 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值 9 ); 10 }
Url格式为:http://localhost:80/home/index 对应规则为:{controller}/{action}/{id} 黑体部分就是对应部分。这还是有默认值的情况。
详细匹配应该为:http://localhost:80/Custom/Detials/1 。去掉主机域名,剩下的对应就是匹配Controller和Action了。通过Routing组件解析这个Url,Controller是Custom,Action是Detials。传递过去的Id是1。这就是使用了MapRoute( string name, string url, object defaults)
;这个方法的重载。
实例二:不使用默认值的Url路由规则
函数签名:MapRoute( string name, string url);
routes.MapRoute("没有默认值路由规则", "{controller}/{id}-{action}");
适合的Url例子:http://localhost:80/Custom/1-Detials
它将不匹配http://localhost:80/
实例三:带名称空间的Url路由规则
函数签名:MapRoute( string name, string url, string[] namespaces);//路由名,Url规则,名称空间
1 routes.MapRoute( 2 "MyUrl", // 路由名称 3 "{controller}/{id}-{action}", // 带有参数的 URL 4 new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // 参数默认值 5 new string[] { "MvcDemo.Controllers" }//命名空间 6 );
Url:http://localhost:0000/Custom/1-Detials
这个例子是带命名空间的路由规则,这在Aeras使用时非常有用。
实例四:带约束的路由规则
函数签名:MapRoute( string name, string url, object defaults, object constraints);//路由名,Url规则,默认值,名称空间
1 routes.MapRoute( 2 "Rule1", 3 "{controller}/{action}-{Year}-{Month}-{Day}}", 4 new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" }, 5 new { Year = @"^\d{4}", Month = @"\d{2}" } 6 );
实例五:带名称空间,带约束,带默认值的路由规则
函数签名:MapRoute( string name, string url, object defaults, object constraints, string[] namespaces);
1 routes.MapRoute( 2 "Rule1", 3 "Admin/{controller}/{action}-{Year}-{Month}-{Day}", 4 new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" }, 5 new { Year = @"^\d{4}", Month = @"\d{2}" }, 6 new string[] { "MvcDemo.Controllers" } 7 );
Url:http://localhost:14039/Admin/home/index-2010-01-21
实例六:捕获所有的路由
1 routes.MapRoute( 2 "All", // 路由名称 3 "{*Vauler}", // 带有参数的 URL 4 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值 5 );
1 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");//是忽略这个规则的Url
1 AreaRegistration.RegisterAllAreas();//注册所有的Areas 2 RegisterRoutes(RouteTable.Routes);//注册自定义规则 3 4 ////调试用语句,需要下载RouteDebug.dll,并添加引用!加入这句话后就可以测试Url路由了。 5 //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
—— EOF ——
作者:悠扬的牧笛
地址:https://www.cnblogs.com/xhb-bky-blog/p/6185206.html
声明:本博客原创文字只代表本人工作中在某一时间内总结的观点或结论,与本人所在单位没有直接利益关系。非商业,未授权贴子请以现状保留,转载时必须保留此段声明,且在文章页面明显位置给出原文连接。
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步