本篇文章是初识Asp.Net MVC2.0 的后续的介绍。此文将从Routing未开放源代码开始,还会对Asp.Net的Url重写技术做个简单介绍。并简单介绍Asp.Net MVC2.0 Url路由技术的使用。
Url路由是MVC很重要的核心的一部分。不过它是单独于MVC项目,而且没有开源。它作为微软的一个独立的项目抽取了出来,在Asp.Net MVC中只是引用了这个dll。新建一个MVC Web项目,打开Global.asax。我们看到系统帮我们添加了using System.Web.Routing 引用。Routing作为一个单独的dll被我们的项目引用。微软并没有对Routing进行开放源代码。所以:Asp.Net MVC是开源的,但是Routing不是开源的!
首先讲一下Asp.Net的Url重写技术。一种最简单实现Url重写技术就是在Global.asax文件中
protected void Application_BeginRequest(object sender, EventArgs e) {
string originalUrl = Request.Url.PathAndQuery;//获取原先的Url请求(假的请求)
//转化成真的url请求....
HttpContext.Current.RewritePath(newurl);//将针对Url请求交给HttpContext
}
举个例子:一个假的Url地址:http://www./news/sports-2009-1-1-201.html
看到上面的那个url其实很多情况下那不是一个真正的url,不一定存在sports-2009-1-1.html这么一个html页。我们如果在Application_BeginRequest事件中将这个url重写成这样:
http://www./news/sports.aspx?date=2009-1-1&id=201 这才是真正的url!
现在开始讲Asp.Net MVC的路由机制,首先要有个观念就是:Asp.Net MVC的url地址是表现的web程序的一个逻辑的结构。新建一个Asp.Net MVC Web项目。然后按F5直接可以运行。页面会自动跳转到http://localhost:端口号/Home/Index页面。玄机还是在Global.asax中。看下面代码:
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 }, // 参数默认值 new string[] { "MvcDemo.Controllers" } );
Global.asax中的RegisterRoutes方法中注册了一个名字叫:Default的路由,格式是: http://localhost:端口号/controller/action 并添加了这条路由的默认值new { controller = "Home", action = "Index"}。所以我们按F5启动的时候, 地址栏是:http://localhost:端口号/ 这个样子,则匹配到我们的这条路由,并没有具体值,就匹配了我们的默认值。 页面会则自动跳转到http://localhost:端口号/Home/Index页面
本篇主要讲述Routing组件的作用,以及举几个实例来学习Asp.Net MVC2.0 Url路由技术。 接着上一篇开始讲,我们在Global.asax中注册一条路由后,我们的请求是怎么转到相应的View的呢?Controller和Action是怎么解析的?这就是Routing组件干的事情了。 Routing的作用:它首先是获取到View传过来的请求,并解析Url请求中Controller和Action以及数据,其次他将识别出来的数据传递给Controller的Action(Controller的方法)。这是Routing组件的两个重要的作用! 下面我们从几个例子来讲解一下Url路由的使用。 MapRoute()有6个方法可以重载,下面举5个例子相应介绍! 实例一:首先讲解的是系统默认提供的路由格式,下面是系统给的默认代码: 1 2 3 4 5 6 7 8 9 10 | 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路由了。
|