如何在Web Form中使用URL Routing?

什么是URL Routing?

所谓URL RoutingURL路由),指的是在Web中,URL指向的不再是某个物理文件,而是一个说明有关URL路由的字符串,开发者可以自定义该字符串的格式。在默认情况下,URL Routing在ASP.NET应用程序中是可以直接使用的,但在ASP.NET站点上,需要做一些配置才能使用。

 

为什么要使用URL Routing?

在使用URL Routing前,我们的URL可能是http://www.mysite.com/profile.aspx?userid=1,使用URL Routing后,我们的URL可以变成http://www.mysite.com/profile/1。修改后的URL更加友好,更有利于SEO。至于其它目的嘛,在使用过程中再慢慢挖掘吧。

 

URL Routing只能在MVC中才能使用吗?

路由程序集(System.Web.Routing.dll)在.NET Framework V3.5 sp1中就包含了,而MVC是在之后才发布的。因此不能说URL Routing只能在MVC中才能使用。不过在MVC中增加了Routing在一些扩展方法(包含在System.Web.MvcRouteCollectionExtemsion类中),使用起来更加方便。


下面简单介绍下如何在Web Form中使用URL Routing。

1. 添加对程序集System.Web.Abstractions.dll,System.Web.Routing.dll的引用

 

2. 添加一个IRouteHandler的实现类CustomRouteHanlder

CustomRouteHandler
 1 using System.Web;
 2 using System.Web.Compilation;
 3 using System.Web.Routing;
 4 using System.Web.UI;
 5 
 6 public class CustomRouteHandler : IRouteHandler
 7 {
 8     public string VirtualPath
 9     {
10         get;
11         private set;
12     }
13 
14     public CustomRouteHandler(string virtualPath)
15     {
16         this.VirtualPath = virtualPath;
17     }
18 
19     #region IRouteHandler Members
20 
21     public IHttpHandler GetHttpHandler(RequestContext requestContext)
22     {
23         var page = BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(Page)) as IHttpHandler;
24         return page;
25     }
26 
27     #endregion
28 }


3. 配置web.config文件

1 <httpModules>
2     <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
3 </httpModules>


4. 在Global.asax页面中注册路由表

    4.1 添加名称空间引用   

1 <%@ Import Namespace="System.Web.Routing" %>


    4.2 在Application_Start事件中注册路由表

Global.asax
1     void Application_Start(object sender, EventArgs e) 
2     {
3         RegisterRoutes(RouteTable.Routes);
4     }
5 
6     void RegisterRoutes(RouteCollection routes)
7     {
8         routes.Add("Demo1"new Route("Demo1/"new CustomRouteHandler("~/WebForm1.aspx")));
9     }


    4.3 如何设定URL Routing参数

1     void RegisterRoutes(RouteCollection routes)
2     {
3         routes.Add("Demo2"new Route("Demo2/{action}"new CustomRouteHandler("~/WebForm2.aspx")));
4     }


    4.4 如何设定URL Routing参数的黙认值

global.asax
1     void RegisterRoutes(RouteCollection routes)
2     {
3         routes.Add("Demo3"new Route("Demo3/{action}"new RouteValueDictionary { { "action""show" } }, new CustomRouteHandler("~/WebForm3.aspx")));
4     }


    4.5 如何在路由目标页面使用URL里的参数

         4.5.1 修改自定义类CustomRouteHanlder

CustomRouteHandler
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            
// 从ReqestContext对象中获取URL参数,并把值写到HttpContext的Items集合中供路由目标页面使用
            foreach (var urlParm in requestContext.RouteData.Values)
            {
                requestContext.HttpContext.Items[urlParm.Key] 
= urlParm.Value;
            }

            var page 
= BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(Page)) as IHttpHandler;
            
return page;
        }


        4.5.2 在路由目标页面中读取URL参数

<%= this.Context.Items["action"%>


    4.6 如何为URL参数添加约束

global.asax
1     void RegisterRoutes(RouteCollection routes)
2     {
3         routes.Add("Demo4"new Route("Demo4/{action}"new RouteValueDictionary { { "action""show" } }, new RouteValueDictionary { { "action"@"\w{4}" } }, new CustomRouteHandler("~/WebForm4.aspx")));
4     }

    如代码所示,要求action参数值必须是4个字符,若action参数长度不等于4个字符,则会得到“无法找到资源”的错误提示。

 

点击下载示例代码 

posted @ 2010-07-02 20:21  Jailu  阅读(2871)  评论(0编辑  收藏  举报