Asp.Net MVC2.0 Url路由配置
2012-01-09 21:43
681 查看
Routing的作用:它首先是获取到View传过来的请求,并解析Url请求中Controller和Action以及数据,其次他将识别出来的数据传递给Controller的Action(Controller的方法)。这是Routing组件的两个重要的作用!
下面我们从几个例子来讲解一下Url路由的使用。
MapRoute()有6个方法可以重载,下面举5个例子相应介绍!
实例一:首先讲解的是系统默认提供的路由格式,下面是系统给的默认代码:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
);
}
Url格式为:http://localhost:0000/home/index
对应规则为:{controller}/{action}/{id} 黑体部分就是对应部分。这还是有默认值的情况。
详细匹配应该为:http://localhost:0000/Custom/Detials/1
去掉主机域名,剩下的对应就是匹配Controller和actiong了。通过Routing组件解析这个Url,Controller是Custom,Action是Detials。传递过去的Id是1。
这就是使用了MapRoute( string name, string url, object defaults);这个方法的重载。
实例二:不使用默认值的Url路由规则
函数头:MapRoute( string name, string url);
routes.MapRoute("没有默认值路由规则", "{controller}/{id}-{action}");
适合的Url例子:http://localhost:0000/Custom/1-Detials
它将不匹配http://localhost:0000/
实例三:带名称空间的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[] { "MvcDemo.Controllers" }//命名空间
);
Url:http://localhost:0000/Custom/1-Detials
这个例子是带命名空间的路由规则,这在Aeras使用时非常有用。不多说,后面再说!
实例四:带约束的路由规则
函数头: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
实例五:带名称空间,带约束,带默认值的路由规则
函数头: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
实例六:捕获所有的路由
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路由了。
下面我们从几个例子来讲解一下Url路由的使用。
MapRoute()有6个方法可以重载,下面举5个例子相应介绍!
实例一:首先讲解的是系统默认提供的路由格式,下面是系统给的默认代码:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
);
}
Url格式为:http://localhost:0000/home/index
对应规则为:{controller}/{action}/{id} 黑体部分就是对应部分。这还是有默认值的情况。
详细匹配应该为:http://localhost:0000/Custom/Detials/1
去掉主机域名,剩下的对应就是匹配Controller和actiong了。通过Routing组件解析这个Url,Controller是Custom,Action是Detials。传递过去的Id是1。
这就是使用了MapRoute( string name, string url, object defaults);这个方法的重载。
实例二:不使用默认值的Url路由规则
函数头:MapRoute( string name, string url);
routes.MapRoute("没有默认值路由规则", "{controller}/{id}-{action}");
适合的Url例子:http://localhost:0000/Custom/1-Detials
它将不匹配http://localhost:0000/
实例三:带名称空间的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[] { "MvcDemo.Controllers" }//命名空间
);
Url:http://localhost:0000/Custom/1-Detials
这个例子是带命名空间的路由规则,这在Aeras使用时非常有用。不多说,后面再说!
实例四:带约束的路由规则
函数头: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
实例五:带名称空间,带约束,带默认值的路由规则
函数头: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
实例六:捕获所有的路由
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路由了。
相关文章推荐
- Asp.Net MVC2.0 Url 路由入门---实例篇
- Asp.Net MVC2.0 Url 路由入门---实例篇 【转】
- Asp.Net MVC2.0 Url 路由入门---实例篇
- Asp.Net MVC2.0 Url路由
- Asp.Net MVC2.0 Url 路由入门
- Asp.Net MVC2.0 Url 路由入门
- Asp.Net MVC2.0 Url 路由入门---实例篇
- ASP.NET MVC路由配置
- ASP.NET MVC路由配置
- IIS7.5(经典/集成),IIS6,asp.net 4.0下配置Url映射(asp.net mvc)
- ASP.NET MVC 路由配置 管线模式伪静态
- 史上最全的ASP.NET MVC路由配置
- 转:在ASP.NET MVC中通过URL路由实现对多语言的支持
- ASP.NET MVC 5 Web编程2 -- URL映射(路由原理)
- IIS7.5(经典/集成),IIS6,asp.net 4.0下配置Url映射(asp.net mvc)
- ASP.NET MVC路由扩展:链接和URL的生成
- IIS7 配置大全(ASP.NET 2.0, WCF, ASP.NET MVC,php)
- asp.net mvc 伪静态路由配置
- 史上最全的ASP.NET MVC路由配置
- asp.net web form 使用URL路由 注不是mvc中的路由