zoukankan      html  css  js  c++  java
  • ASP.NET MVC的路由

    ASP.NET MVC的路由

    好久没写博文了,感觉最近好像少了点动力。唉!这回就看看这个MVC的路由。

      说这个路由机制其实不是MVC里面特有的,ASP.NET里面本身就有的,只不过在WebForm里面一般比较少用,而在MVC里就是把原本的路由扩展了。原本对不知道单纯在ASP.NET里使用路由的详细情况,但自从看了蒋金楠老师的几篇文章之后知晓了,不过这篇还是讲MVC的路由而已。

      路由的定义是位于根目录下的全局文件Global.asax.cs。里面有个RegisterRoutes方法

    复制代码
     1         public static void RegisterRoutes(RouteCollection routes)
     2         {
     3             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
     4 
     5             routes.MapRoute(
     6                 "Default", // Route name
     7                 "{controller}/{action}/{id}", // URL with parameters
     8                 new { controller = "System", action = "TestPage", id = UrlParameter.Optional } // Parameter defaults
     9             );
    10         }
    复制代码

    这里有两部分,一部分是

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    这个是用于忽略路由的,按上面的情况,则是不对asd文件经行路由,可以直接去访问。

    复制代码
    1             routes.MapRoute(
    2                 "Default", // Route name
    3                 "{controller}/{action}/{id}", // URL with parameters
    4                 new { controller = "System", action = "TestPage", id = UrlParameter.Optional } // Parameter defaults
    5             );
    复制代码

    这个是定义路由的。根据上面的注释,可以看出第一个参数是定义了路由的名称;第二个则是URL的参数;第三个则是URL参数的默认值。除此外还可以对URL的参数进行某些约束,设置命名空间等。关于MapRoute的其他重载如下

    复制代码
    1 public static Route MapRoute(this RouteCollection routes, string name, string url);
    2 public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults);
    3 public static Route MapRoute(this RouteCollection routes, string name, string url, string[] namespaces);
    4 public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints);
    5 public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, string[] namespaces);
    6 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,这个需要额外引用一个dll,叫RouteDebug.dll(可在网上找一下),然后在全局文件Global.asax.cs里面的Application_Start()方法里加多一行代码

    RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);

    打开浏览器输入相应的URL,则会出现一下页面

      从这个页面可以知道当前的URL跟那个路由匹配了,还有与当前路由列表中其他路由的匹配情况。除此之外,能获取到当前请求的URL,匹配的各个参数,还有各个路由在路由集合的顺序等等。

    在这个例子中我还定义了另一个路由

    复制代码
    1             routes.MapRoute(
    2                 "MyRoute1",
    3                 "Customer/{action}/{id}",
    4                 new { controller = "Customer", action = "Login", id = -1 });
    复制代码

      从上面路由集合的顺序可以看出,这个路由的顺序要比默认路由{Controller}/{action}/{*id}有前。如果这个路由放在默认路由前面,则这个MyRoute1路由则起不了作用,可以用“~/Customer”测试。

     
     
    分类: MVC
  • 相关阅读:
    计算机考研复试真题 数字求和
    计算机考研复试真题 简单计算器
    计算机考研复试真题 N阶楼梯上楼问题
    P1082 [NOIP2012 提高组] 同余方程
    进制转换
    浮点数加法
    N的阶乘
    1055 The World's Richest (25 分)
    1028 List Sorting (25 分)
    1062 Talent and Virtue (25 分)
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3168808.html
Copyright © 2011-2022 走看看