码迷,mamicode.com
首页 > Web开发 > 详细

MVC中url路由规则

时间:2015-01-12 19:11:41      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:

Routing:首先获取视图页面传过来的请求,并接受url路径中的controller和action以及参数数据,根据规则将识别出来的数据传递给某controller中的某个action方法

MapRoute()有6个方法可以重载

方法1:系统提供的默认路由规则格式

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",//路由名称
                url: "{controller}/{action}/{id}",//带有参数的url
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }//参数默认值
            );
        }

 url格式为:http://localhost:0000/Home/Index 对于规则为{controller}/{action}/{id} 黑色部分就是对应部分 

详细匹配为:http://localhost:0000/Home/Index/1  通过Routing组件分析该url  controller为Home  action为Index 参数为Id为1 使用了MapRoute( string name, string url, object defaults);这个方法的重载。

 

方法:2:不使用默认值的url路由规则

函数头:MapRoute( string name, string url);

 routes.MapRoute("没有默认值路由规则", "{controller}/{id}-{action}");

适合的Url例子:http://localhost:0000/Custom/1-Detials  

它将不匹配http://localhost:0000/

 

方法3:带有命名空间的Url路由规则

 函数头:MapRoute( string name, string url, string[] namespaces);//路由名,Url规则,名称空间

routes.MapRoute(

"myurl",//路由名称

"{controller}/{id}-{action}",//带有参数的url

new{controller="Home",action="Index",id=UrlParameter.Optional},//参数默认值

new string[]{"mymvc.Controllers"}//命名空间

);

Url:http://localhost:0000/Custom/1-Detials
       这个例子是带命名空间的路由规则,这在Aeras使用时非常有用。

 

方法4:带有约束的路由规则

  函数头:MapRoute( string name, string url, object defaults, object constraints);//路由名,Url规则,默认值,名称空间

routes.MapRoute(
                "Rule1",
                "{controller}/{action}-{Year}-{Month}-{Day}}",
                new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" },
                new { Year = @"^\d{4}", Month = @"\d{2}" }
            );

       Url:http://localhost:14039/home/index-2010-01-21

 

方法5:带有命名空间,约束,待默认值的路由规则

函数头:MapRoute( string name, string url, object defaults, object constraints, string[] namespaces);

        routes.MapRoute(
                "Rule1",
                "Admin/{controller}/{action}-{Year}-{Month}-{Day}",
                new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" },
                new { Year = @"^\d{4}", Month = @"\d{2}" },
                new string[] { "MvcDemo.Controllers" }
            );

        Url:http://localhost:14039/Admin/home/index-2010-01-21

 

方法6:捕获所有的路由

routes.MapRoute(
                "All", // 路由名称
                "{*Vauler}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );

 

关于Global.asax剩余部分的说明:

       routes.IgnoreRoute("{resource}.axd/{*pathInfo}");是忽略这个规则的Url

   AreaRegistration.RegisterAllAreas();//注册所有的Areas
       RegisterRoutes(RouteTable.Routes);//注册我们写的规则
       //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);//调试用语句,需要下载RouteDebug.dll,并添加引用!加入这句话后就可以测试Url路由了。

MVC中url路由规则

标签:

原文地址:http://www.cnblogs.com/xishuqingchun/p/4218977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!