asp.net mvc 路由功能


一:默认显示area下某个文件

 routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Main", action = "Index", id = UrlParameter.Optional }
            ).DataTokens.Add("area", "EnWeb");//默认area 的控制器名称     

http://blog.csdn.net/kai161/article/details/19189681

Area下面会生成一个映射文件EnWebAreaRegistration


二:routes是满足url规则则会执行上面对应,先在上面先匹配

   

    1:url为空默认执行

routes.MapRoute(
            name: "AA",
            url: "",
            defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
            );  
     此时如何url为空会执行Login控制器下Index方法


    2 控制器+参数的url格式,无需传action  

routes.MapRoute(
            name: "BB",
            url: "{controller}/{id}",
            defaults: new { controller = "CompanysLog", action = "Index", id = UrlParameter.Optional }
            );    
    此时只要满足{controller}/{id}这种url格式就会执行

    CompanysLog/Index

    url规则写死也行,写死可以防止有些规则重复

 routes.MapRoute(
            name: "MM",
            url: "{CompanysLog}/{id}",
            defaults: new { controller = "CompanysLog", action = "Index", id = UrlParameter.Optional }
            );    
     这时url必须是CompanysLog/XXX 这种格式才会执行配置的控制器与Action

      3:

使用{*catchall}捕捉超出数量的segment


例如,对于这条route:


routes.MapRoute("MyRoute","{controller}/{action} /{*catchall}",
new { controller = "Home", action ="Index",
id = UrlParameter.Optional });
由于使用了{*catchall},对于url:


http://mysite/Home/Index/All/More/More/More


此时,controller=Home,Action=Index, catchall=”All/More/More/More”


这样,就把解析剩下segment的工作交给了自己处理

 

三:返回某个页面

areas下面views


或者直接的views

 return View("~/Views/Price/AirPrice.cshtml");


四:隐藏controller,一级目录访问

   

    匹配前缀:

     routes.MapRoute(
            name: "AA",
            url: "{action}",
            defaults: new { controller = "Home", action = "Index" },
            constraints: new { action = "^H.*" }
            );

      这里凡是是H开头的ation默认的controller就是home

        routes.MapRoute(
                name: "BB",
                url: "{action}",
                defaults: new { controller = "Test", action = "Index" },
                constraints: new { action = "^A.*" }
                );

      同理以A开头的ation默认的controller就是Test  

   匹配后缀:

       routes.MapRoute(
            name: "AA",
            url: "{action}",
            defaults: new { controller = "Price", action = "fclfreight" },
            constraints: new { action = "^.*freight" }
            );

       匹配以freight结尾的调用名称为price的controller



http://www.tuicool.com/articles/NjEfIrY




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值