网络上关于asp.net路由知识的一些资源:
https://www.cnblogs.com/cklovefan/p/7785307.html
https://blog.youkuaiyun.com/slowlifes/article/details/72461440
一、请求一个ASP.NET mvc的网站和以前的web form是有区别的,ASP.NET MVC框架内部给我们提供了路由机制,当IIS接受到一个请求时,当 未设置RouteTable.Routes.RouteExistingFiles 属性时,asp.net 会先看是否请求了一个静态资源(.html,css,js,图片等),如果不是则说明是请求的是一个动态页面,就会走asp.net的管道,mvc的程序请求都会走路由系统,会映射到一个Controller对应的Action方法。下面通过一个ASP.NET MVC5项目来详细介绍一下APS.NET MVC5路由系统的机制。
asp.net 程序启动时,会执行global.asax文件中的Application_Start方法,实现区域、路由、过滤器等全局对象注册;
protected void Application_Start()
{
//===============注册区域===============
AreaRegistration.RegisterAllAreas();
//===========注册过滤器======================
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
//===========注册路由======================
RouteConfig.RegisterRoutes(RouteTable.Routes);
//==============注册css、js文件绑定==================
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
路由注册
public static void RegisterRoutes(RouteCollection routes)
{
//===============静态路径访问也需要提交到路由处理程序=======================
RouteTable.Routes.RouteExistingFiles = true;
//===============不需要传递到asp.net管道的Url=================
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("Content/{*catchall}");
routes.IgnoreRoute("Scripts/{*catchall}");
//=================需要把自定义的静态路由放到缺省路由前面才能确保路由解析准确,为啥子?bug?===
Route testRoute= routes.MapRoute("MyRoute", "grids/list", new { controller = "Grid", action = "ListGrid" });
//routes.Add()
//===========定义缺省的路由处理程序=======================
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
上述路由定义了两类路由处理:
当url为: grids/list时,将路由传递给命名为Grid 的控制器的ListGrid方法;
其它的url通过缺省的路由映射程序处理,不过比较奇怪的是需要将静态路由处理程序定义在缺省路由之前,否则不能正常的处理访问URL,现在还不清楚是什么原因,why????
注册的缺省路由是三段式的:
Url段的数量 | 实例 | Route映射 |
0 | mydomain.com | controller = Home action = Index |
1 | mydomain.com/Home | controller = Home action = Index |
2 | mydomain.com/Home/Index | controller = Home action = Index |
3 | mydomain.com/Home/Index/All | ID=All |
当需要访问更多分段时,注册类似路由:
routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
匹配的路由如下:
Url段的数量 | 实例 | Route映射 |
0 | mydomain.com | controller = Home action = Index |
1 | mydomain.com/Customer | controller = Customer action = Index |
2 | mydomain.com/Customer/List | controller = Customer action = List |
3 | mydomain.com/Customer/List/All/Delete/Perm | controller = Customer action = List id = All catchall = Delete /Perm |
同一区域、不同命名空间的控制器最好不要重名,如果发生算了重名的情况,通过指定命名空间优先级,设定处理控制器,如:
存在以下不同命名空间的同名控制器:
WebApplication1.Controllers.HomeController
WebApplication1.Controllers1.HomeControlle
如下设置路由:
public static void RegisterRoutes(RouteCollection routes) {
routes.MapRoute("Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "WebApplication1.Controllers" }
);
}
在某些情况下,我们需要将路由限制到特定值,可如下实现:
public static void RegisterRoutes(RouteCollection routes) {
routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "^H.*", action = "^Index$|^About$", httpMethod = new HttpMethodConstraint("GET")},
new[] { "URLsAndRoutes.Controllers"});
}
设置路由只能是H字母开头,方法只能是Index和About,访问方式只能是Get