ASP.NET MVC3 路由系统

1.路由的两个作用:a,检查地址栏中的URL,计算出当前地址请求的是哪个controller 中的 哪个action,并且自动提取请求的参数 。
                            b, 生成URL 。
 
2 路由参数的默认值
?
public   static   void   RegisterRoutes(RouteCollection routes)
{
     routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" );
 
     routes.MapRoute(
     "Default" , // 路由名称
     "{controller}/{action}/{id}" , // 带有参数的 URL
     new   { controller = "Home" , action = "Index" , id =“defaultID”}, // 参数默认值
     );
}
 
 
3 自定义路由模式的例子。生成URL的最简单并且最常用的办法是在页面中通过ActionLink这个辅助方法来完成的。
复制代码
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
                        
   //自定义路由模式
   routes.MapRoute("", // 路由名称
    "Public/{controller}/{action}", // 带有参数的 URL
    new { controller = "Home", action = "Index"}, // 参数默认值
);
 
routes.MapRoute(
    "Default", // 路由名称
    "{controller}/{action}/{id}", // 带有参数的 URL
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // 参数默认值
    );
}
复制代码

 


要注意自定义的路由格式要放在通用格式的前面,因为一旦有路由北匹配上,本次的匹配操作就结束了。
 
4. 自定义参数 ,以及参数的传递
    首先定义一个路由
  
复制代码
 routes.MapRoute(
    "Default", // 路由名称
    "{controller}/{action}/{id}/{age}/{birthday}", // 带有参数的 URL
    new { controller = "Home", action = "Index", id = UrlParameter.Optional ,
              age=18 ,birthday=new DateTime(1989,1,1) }, // 参数默认值
    );

public ActionResult Index( string id , int age , DateTime birthday)
{
    return View();
}    
复制代码
如果我们在路由中定义了定义好了包含这三个参数的模式,然后输入正确的URL后,路由系统就会自动从中提取出controller , action ,以及这三个参数(如果参数名匹配,并且能够完成类型转换的话)。
例如,我们输入  http://mysite.com/User/Getinfo/123344/24/1989-9-16 ,路由就会提取出一下信息 ,controller=User ,action = Getinfo,
id=123344 , age=24 ,birthday=1989-9-16
 
5. 定义可变长度的参数的路由
       
 routes.MapRoute(
    "Default", // 路由名称
    "{controller}/{action}/{id}/{*catchall}", // 带有不定个数的参数的 URL
    new { controller = "Home", action = "Index",    
    id=UrlParameter.Optional     }, // 参数默认值
);
       这个路由能匹配所有的URL。具体匹配情况如下图:
      
序号 URL 示例 映射结果
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  controller=Customer,action=List,id=All
4 mydomain.com/Customer/List/All/Delete  controller=Customer,action=List,id=All,catchall=Delete
5 mydomain.com/Customer/List/All/Delete/Perm  controller=Customer,action=List,id=All,catchall=Delete/Perm
 
6. 定义命名空间的优先级。
    根据以上内容我们已经知道,当URL匹配上一个路由之后,就会提取路由中的信息,然后进行进一步处理。假如现在我们的某个路由提取出了一个controller为Account ,并且我们的解决方案中有不只一个名为“AccountController”的类,由于路由并不知道要调用哪个controller ,这个时候就会出现错误。那么,如何解决这种问题呢?我们可以在注册路由的时候,制定某个路由有限查找controller 的命名空间,如下面的代码所示。
        
复制代码
 routes.MapRoute(
    "Default", // 路由名称
    "{controller}/{action}/{id}", // 带有参数的 URL
    new { controller = "Home", action = "Index", 
    id = UrlParameter.Optional }, // 参数默认值
    new string[] { "MyNamespace.Controllers" } //指定优先命名空间
);
复制代码
    由此可见,控制器的类不一定要放在网站项目中的Controllers命名空间下,也可以单独写一个程序集,如果需要的话。
 
7. 给路由加一些约束条件
    我们可以给URL的匹配加上以下限制:
        a, 正则表达式
        b,HTTP method
    代码如下
   
复制代码
  routes.MapRoute(
    "Default", // 路由名称
    "{controller}/{action}/{id}", // 带有参数的 URL
    new { 
        controller = "Home", 
       action = "Index", 
       id = UrlParameter.Optional }, // 参数默认值
        //设置匹配约束
    new  {controller = "^H.*" , action ="^Index$|^About&" , 
        httpMethod=new HttpMethodConstrain("Get") } ,
   new string[] { "MyNamespace.Controllers" }  //指定优先命名空间
);
            
复制代码
    这个路由就要求,controller的值必须是以字母"H"开头的,并且action只能是Index 或者 About ,并且只能用Get方式。
    如果这些系统定义的约束条件不能满足项目的需求,我们可以自定义约束条件,只要实现 IRouteConstraint 接口 ,然后将该类的一个实例传给routs就可以了。代码如下:
  
复制代码
public class UserConstrain : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, 
    string  parameterName,  RouteValueDictionary values,
    RouteDirection routeDirection)
    {
    return true ; //你可以在这里做具体的操作
    }
}
        
routes.MapRoute(
    "Default", // 路由名称
    "{controller}/{action}/{id}", // 带有参数的 URL
    new { controller = "Home", action = "Index", id 
        =UrlParameter.Optional }, // 参数默认值
    new  {controller = "^H.*" , action ="^Index$|^About&" ,     
        httpMethod=new HttpMethodConstrain("Get") ,
    customConstrain= new  UserConstrain () } , //设置匹配约束
    new string[] { "MyNamespace.Controllers" }  //指定优先命名空间
);
                
复制代码
  
8. 处理磁盘文件的映射。
    在 asp.net webForms 中,请求的地址直接映射到一个磁盘的文件。而在MVC 中,一般都是把请求交给特定的控制器来执行,但是有时也会直接请求某个磁盘文件,如图片、静态HTML文件等。默认情况下,MVC框架是先检测URL能否直接映射到某个磁盘文件,如果不能,然后再把当前请求交给路由系统来处理。如果匹配上了,就是直接绕过路由系统。
    如果我们希望对磁盘文件的映射及一些额外的限制,我们可以改变这个顺序,即 ,让MVC框架先经过路由系统,如果没有匹配上,再去检测磁盘文件有没有匹配。这样子我们就可以充分利用强大的路由系统了。我们只需要把 rounts 的 RouteExistingFiles 属性设为true就可以改变这个顺序了。
 
复制代码
public static void RegisterRoutes(RouteCollection routes)
{
        routes.RouteExistingFiles = true;
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
        routes.MapRoute(
            "Default", // 路由名称
           "{controller}/{action}/{id}", // 带有参数的 URL
            new { controller = "Home", action = "Index", id = 
                UrlParameter.Optional }, // 参数默认值
        );
}                
复制代码

 


    
9. 由路由生成URL。
    9.1 通过ActionLink生成超链接。 跟通过URL进行路由映射一样,由路由生成HTML也需要进行路由匹配,并且,只要遇到第一个匹配成功,以后的路由就会被忽略。下面看几个简单的例子。生成URL的最简单并且最常用的办法是在页面中通过ActionLink这个辅助方法来完成的。
    @Html.ActionLink("Click to About Page","About") //指定当前控制器的其他 action
    @Html.ActionLink("Click to About Page","About",“myController”)  //指定 controller 和 action
    @Html.ActionLink("Click to About Page","About",“myController”, new { id =  "myId" } )  //指定 controller 、action 以及其他参数
    @Html.ActionLink("Click to About Page","About",“myController”, new { id =  "myId" ,otherVal =value } )  //指定 controller 、action 以及其他参数
    @Html.ActionLink("Click to About Page","About",“myController”, new { id =  "myId" ,otherVal =value }  , new { id=myAnchorID , @class     ="myCss"})
    如果系统只包括默认路由的话,第三个重载会生成  <a  href= " myController/About/myId">Click to About Page</a>
    第四个重载由于默认的路由不包括 otherVal 参数,所以会以查询字符串的形式出现, <a  href= " myController/About/myId">Click to About Page</a>
    第五个重载加上了一些HTML Attributes  <a  href= " myController/About/myId" id="myAnchorID " class='myCss'>Click to About Page</a>
 
    9.2 如果希望生成不带超链接的URL,可以使用 Action
        @Url.Action("About","myController" , new { id = "myId" })
        生成如下URL myController/About/myId
    
    9.3 通过路由生成超链接
    @Html.RouteLink("Click to System About Page",new {controller="myController",action="About",id = "myId" })
 
    10.  区域(Area)
   区域,在企业开发中还是比较常见的,一般用于子系统的开发,每个子系统都是一个完整的MVC框架,一个子系统就是一个区域(Area)
        10.1 新建一个区域
        在建好的MVC项目的解决方案资源管理器中,右击项目,选择 添加=>区域 ,然后输入Admin ,则MVC框架会自动处理剩下的所有工作,包括生成解决方案中的相关目录和文件,生成 AdminAreaRegistration 类,在Global文件中注册所有区域等。每个区域都是一个独立的小系统,可以独立地完成很多工作。
        10.2 如果我们组要在一个特定的区域内生成本区域的Url,我们不需要考虑区域的因素,MVC框架会自动检测到当前区域,并且把区域信息添加到URL中。如果我们要链接到其他区域,或链接到没有区域的地方,可以使用@Html.ActionLink方法,把area当做一个参数传递过去即可。
    链接到本区域:@Html.ActionLink("Click to Index","Index")
    链接到Admin区域:@Html.ActionLink("Click to Index","Index",new {area="Admin"})
    链接到无区域:@Html.ActionLink("Click to Index","Index",new {area=""})
    
    由于MVC框架先注册了所有区域,又注册了所有路由,所以,在这里,area 只是一个路由中的参数,这个参数会被路由恰当地处理成我们需要的链接,也会从特定的链接中提取出恰当的参数。
 
    总之一句话,MVC框架中的路由系统,那是相当强大!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值