ASP.NET MVC的路由
2013-07-02 23:12
357 查看
说这个路由机制其实不是MVC里面特有的,ASP.NET里面本身就有的,只不过在WebForm里面一般比较少用,而在MVC里就是把原本的 路由扩展了。原本对不知道单纯在ASP.NET里使用路由的详细情况,但自从看了蒋金楠老师的几篇文章之后知晓了,不过这篇还是讲MVC的路由而已。
路由的定义是位于根目录下的全局文件Global.asax.cs。里面有个RegisterRoutes方法
这里有两部分,一部分是
这个是用于忽略路由的,按上面的情况,则是不对asd文件经行路由,可以直接去访问。
这个是定义路由的。根据上面的注释,可以看出第一个参数是定义了路由的名称;第二个则是URL的参数;第三个则是URL参数的默认值。除此外还可以对URL的参数进行某些约束,设置命名空间等。关于MapRoute的其他重载如下
另外,各个路由的名称一定要唯一的,不能重复。
URL参数中{Controller}和{action}是两个比较特殊的占位符,分别代表着请求对应的控制器和行为方法。在后面跟着的参数则 是传到行为方法里面的参数,在这里声明的是{id},方法的参数名一定是id才能获取到参数,否则传到方法里面的参数值是null。如果在参数名前面带一 个星号(*),如{*id},这样就会匹配URL后面所有的剩余的参数。
对于上面这个路由的定义,接收到以下请求时,参数的匹配如下
在这里另外推荐一个好东西专门用于测试路由的好东西,RouteDebug,这个需要额外引用一个dll,叫 RouteDebug.dll(可在网上找一下),然后在全局文件Global.asax.cs里面的Application_Start()方法里加多 一行代码
打开浏览器输入相应的URL,则会出现一下页面
从这个页面可以知道当前的URL跟那个路由匹配了,还有与当前路由列表中其他路由的匹配情况。除此之外,能获取到当前请求的URL,匹配的各个参数,还有各个路由在路由集合的顺序等等。
在这个例子中我还定义了另一个路由
从上面路由集合的顺序可以看出,这个路由的顺序要比默认路由{Controller}/{action}/{*id}有前。如果这个路由放在默认路由前面,则这个MyRoute1路由则起不了作用,可以用“~/Customer”测试。
路由的定义是位于根目录下的全局文件Global.asax.cs。里面有个RegisterRoutes方法
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "System", action = "TestPage", id = UrlParameter.Optional } // Parameter defaults ); }
这里有两部分,一部分是
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
这个是用于忽略路由的,按上面的情况,则是不对asd文件经行路由,可以直接去访问。
routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "System", action = "TestPage", id = UrlParameter.Optional } // Parameter defaults );
这个是定义路由的。根据上面的注释,可以看出第一个参数是定义了路由的名称;第二个则是URL的参数;第三个则是URL参数的默认值。除此外还可以对URL的参数进行某些约束,设置命名空间等。关于MapRoute的其他重载如下
public static Route MapRoute(this RouteCollection routes, string name, string url); public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults); public static Route MapRoute(this RouteCollection routes, string name, string url, string[] namespaces); public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints); public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, string[] namespaces); public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints, string[] namespaces);
另外,各个路由的名称一定要唯一的,不能重复。
URL参数中{Controller}和{action}是两个比较特殊的占位符,分别代表着请求对应的控制器和行为方法。在后面跟着的参数则 是传到行为方法里面的参数,在这里声明的是{id},方法的参数名一定是id才能获取到参数,否则传到方法里面的参数值是null。如果在参数名前面带一 个星号(*),如{*id},这样就会匹配URL后面所有的剩余的参数。
对于上面这个路由的定义,接收到以下请求时,参数的匹配如下
请求URL | 参数 | 备注 |
http://localhost:1345/System/TestPage | Controller=System Action=TestPage | 调用SystemController下的TestPage方法 |
http://localhost:1345/Customer/Login | Controller=Customer Action=Login | 调用CustomerController下的Login方法,id参数为空 |
http://localhost:1345/Customer/Login/1 | Controller=Customer Action=Login Id=1 | 调用CustomerController下的Login方法,id参数为1 |
http://localhost:1345 | Controller=null Action=null | 默认调用SystemController下的TestPage方法 |
http://localhost:1345/System | Controller=System Action=null | 访问SystemController,默认调用TestPage方法 |
RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
打开浏览器输入相应的URL,则会出现一下页面
从这个页面可以知道当前的URL跟那个路由匹配了,还有与当前路由列表中其他路由的匹配情况。除此之外,能获取到当前请求的URL,匹配的各个参数,还有各个路由在路由集合的顺序等等。
在这个例子中我还定义了另一个路由
routes.MapRoute( "MyRoute1", "Customer/{action}/{id}", new { controller = "Customer", action = "Login", id = -1 });
从上面路由集合的顺序可以看出,这个路由的顺序要比默认路由{Controller}/{action}/{*id}有前。如果这个路由放在默认路由前面,则这个MyRoute1路由则起不了作用,可以用“~/Customer”测试。
相关文章推荐
- 传递额外的值 Passing Extra Values |在视图中生成输出URL | 高级路由特性 | 精通ASP-NET-MVC-5-弗瑞曼
- Asp.Net MVC2.0 Url 路由入门
- Asp.Net MVC 路由引擎 Routing
- 为ASP.NET MVC及WebApi添加路由优先级
- 认识 ASP.NET 3.5 MVC 路由 创建自定义路由
- asp.net mvc 通过修改路由规则来实现页面的URL多参数传递
- 认识 ASP.NET 3.5 MVC 路由 在WebForm项目中使用路由
- ASP.NET MVC 路由调试工具Router Debugger
- ASP.NET MVC如何设置路由启动
- ASP.NET MVC2.0学习笔记:路由设置
- ASP.NET没有魔法——ASP.NET MVC路由
- 返璞归真 asp.net mvc (2) - 路由(System.Web.Routing)
- Asp.Net MVC 路由
- ASP.NET MVC 路由
- ASP.Net MVC开发基础学习笔记:三、Razor视图引擎、控制器与路由机制学习
- Mvc多级Views目录 asp.net mvc4 路由重写及 修改view 的寻找视图的规则
- ASP.NET MVC 路由机制
- Asp.Net MVC2.0 Url路由