ASP.net 的URL路由选择(System.Web.Routing.dll)

System.Web.Routing是.net 3.5sp1中新增的一个dll,用它提拱的类可以很方便的实现url的映射,在asp.net WebFrom的编程中可以使客户端请求的URL变得更加的"酷",当然它的最大作用应该还是服务于ASP.net MVC的框架中。

         UrlRouting 的实现原理实际上并不复杂,在System.Web.Routing空间中有一个类 UrlRoutingModule,它实现了IHttpModule接口,并订阅了HttpApplication对象的 PostResoleRequestCache与PostMapRequestHandler两个事件,在第一个事件中UrlRoutingModule 为本次请求准备一个HttpHandler并在第二个事件中将HttpHandler交给HttpContext。其中 PostResolveRequestCache的部分代码如下:

 

private void OnApplicationPostResolveRequestCache(object sender, EventArgs e)
    {
        HttpContextBase context = new HttpContextWrapper(((HttpApplication) sender).Context);
        this.PostResolveRequestCache(context);
    }

  public virtual void PostResolveRequestCache(HttpContextBase context)
    {
        RouteData routeData = this.RouteCollection.GetRouteData(context);
        if (routeData != null)
        {
            IRouteHandler routeHandler = routeData.RouteHandler;  
            if (routeHandler == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, RoutingResources.UrlRoutingModule_NoRouteHandler, new object[0]));
            }
            if (!(routeHandler is StopRoutingHandler))
            {
                RequestContext requestContext = new RequestContext(context, routeData);
                IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext);
                if (httpHandler == null)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, RoutingResources.UrlRoutingModule_NoHttpHandler, new object[] { routeHandler.GetType() }));
                }
                RequestData data2 = new RequestData();
                data2.OriginalPath = context.Request.Path;
                data2.HttpHandler = httpHandler;
                context.Items[_requestDataKey] = data2;
                context.RewritePath("~/UrlRouting.axd");
            }
        }
    }
代码说明:

1.   HttpContextBase是一个抽象类,它的成员与HttpContext是完全一样的,HttpContextWrapper继承于 HttpContextBase,并且将HttpContext对象作为自己的一个成员,它的方法实现完全是依赖于HttpContext对象来实现的。 HttpContextBase与HttpContextWrapper都是在System.Web.Abstractions.dll中,这个DLL也 是.net sp1新增的,它的主要作用是服务于asp.net 的单元测试。

2.  RouteData是与于当前请求URL配置相对应的一个路由数据。URL路由类Route是要我们来配置的并将其添加到 RouteTable的Routes集合中,RouteTable使用了SingleTon设计模式,所以它是线程安全的并且整个应用程序共用一个 RouteTable对象,因此我们要向RouteTable添加Route时必须在Global.asax的Application_Start事件中 完成。

3.   方法从RouteData中获取获取一个RouteHandler对象,这个对象是IRouteHandler接口的实现,在.net sp1中并没有可以直接使用的IRouteHandler的实现,所以需要我们自己实现,实现它的目的也就是创建一个当前Route的可用的 HttpHandler.

 

PostMapRequestHandler的代码实现:

  private void OnApplicationPostResolveRequestCache(object sender, EventArgs e)
    {
        HttpContextBase context = new HttpContextWrapper(((HttpApplication) sender).Context);
        this.PostResolveRequestCache(context);
    }

    public virtual void PostMapRequestHandler(HttpContextBase context)
    {
        RequestData data = (RequestData) context.Items[_requestDataKey];
        if (data != null)
        {
            context.RewritePath(data.OriginalPath);
            context.Handler = data.HttpHandler;
        }
    }

代码说明:就是将上面获得的HttpHandler对象交给HttpContext的Handler属性。

 

如果我要在ASP.net WebFrom中用使用UrlRoute必须完成三件事:

1.实现IHttpHandler接口

2.实现IRouteHandler接口,根据RequestContext的RouteData属性返回相对应的IHttpHandler对象

3.配置Route路由将IRouteHandler对象赋给Route对象的Handler,并将Route添加到RouteTable的Routes集合中

 

 下面我们就可以在ASP.net WebFrom中使用URLRouting并让我们的URL变得更"酷"(待续……)

 

地址:http://blog.csdn.net/heroyuchao/article/details/4158895

posted @ 2014-04-21 19:14  冲向蓝天  阅读(459)  评论(0编辑  收藏  举报