ASP.NET中,所有的上下文对象(HttpContext,HttpReqeust,HttpResponse…)都没有进行抽象,而且它们都是自我封闭的对象,我们无法对它进行扩展和修改。虽然它们都提供公有构造器,我们可能也可以追溯到请求管道的源头,去自己实例化HttpContext,可是它们的大部分方法都是封闭的,不可重写的,这样使得我们在做多工作的时候无法称心如意,甚至于四处碰壁。
ASP.NET MVC由于要提高可扩展性的可测试性,这就要求这些上下文环境中在测试环境中可以被模拟,甚至于在Web环境中也需要被替换,因此在ASP.NET MVC正式版出来之前的.NET 3.5中,微软率先在Framewrok中增加了ASP.NET MVC所依赖的路由组件(System.Web.Routing.dll),同时也增加了一个对原有ASP.NET上下文对象进行抽象和包装的程序集,这就是System.Web.Abstractions.dll。这个程序集本身的代码非常简单,只是对这些对象的现有接口的抽象和适配包装,但是它对于我们以后对MVC程序的扩展却有着至关重要的影响。在MVC程序中,我们应尽量少用或不用原生的上下文对象,转而使用新包装过的HttpContextBase,HttpRequestBase之类的对象,减少对WEB环境的依赖,这样可以大大提高可测试性,同时也可以扩展我们程序的可扩展性。
下面是在Kooboo中,需要替换请求上下文对象的一个例子。在Kooboo中,根据要求,我们需要在路由解析之前做一些事情,修改相应的请求环境,因此我们需要替换和修改HttpRequestBase对象,这个对象是被包含在HttpContextBase中实例化,因此我们只要替换HttpContextBase这个对象的实现(默认实例为:HttpContextWrapper),接下来的事情就都在我们的掌控之中。
首先我们找到实例化HttpContextWrapper的源头之地,在System.Web.Routing.UrlRoutingModule中,我们可以找到实例化HttpContextWrapper的代码,并且我们也可以看到在它的PostResolveRequestCache函数中,它头一句代码就是去解析路由,以下是System.Web.Routing.UrlRoutingModule的完整代码:
[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)] public class UrlRoutingModule : IHttpModule { // Fields private static readonly object _requestDataKey = new object(); private RouteCollection _routeCollection; // Methods protected virtual void Dispose() { } protected virtual void Init(HttpApplication application) { application.PostResolveRequestCache += new EventHandler(this.OnApplicationPostResolveRequestCache); application.PostMapRequestHandler += new EventHandler(this.OnApplicationPostMapRequestHandler); } private void OnApplicationPostMapRequestHandler(object sender, EventArgs e) { HttpContextBase context = new HttpContextWrapper(((HttpApplication) sender).Context); this.PostMapRequestHandler(context); } 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; } } 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() })); } context.Items[_requestDataKey] = new RequestData { OriginalPath = context.Request.Path, HttpHandler = httpHandler }; context.RewritePath("~/UrlRouting.axd"); } } } void IHttpModule.Dispose() { this.Dispose(); } void IHttpModule.Init(HttpApplication application) { this.Init(application); } // Properties public RouteCollection RouteCollection { get { if (this._routeCollection == null) { this._routeCollection = RouteTable.Routes; } return this._routeCollection; } set { this._routeCollection = value; } } // Nested Types private class RequestData { // Properties public IHttpHandler HttpHandler { get; set; } public string OriginalPath { get; set; } } }
我们可以看到,在OnApplicationPostMapRequestHandler和OnApplicationPostResolveRequestCache,都包含有实例化HttpContextWrapper的代码。接下来的问题就很简单了,我们只需要从System.Web.Routing.UrlRoutingModule继承下来,实现一个新的HttpModule,重新定制这两个事件,并且调用相同的PostMapRequestHandler和PostResolveRequestCache,传不同的HttpContextBase对象,问题就解决了:
public class KoobooUrlRoutingModule : UrlRoutingModule { protected override void Init(HttpApplication application) { application.PostResolveRequestCache += new EventHandler(this.OnApplicationPostResolveRequestCache); application.PostMapRequestHandler += new EventHandler(this.OnApplicationPostMapRequestHandler); } private void OnApplicationPostMapRequestHandler(object sender, EventArgs e) { HttpContextBase context = new KoobooHttpContextWrapper(((HttpApplication)sender).Context); this.PostMapRequestHandler(context); } private void OnApplicationPostResolveRequestCache(object sender, EventArgs e) { HttpContextBase context = new KoobooHttpContextWrapper(((HttpApplication)sender).Context); this.PostResolveRequestCache(context); } }
HttpContextBase被替换成了KoobooHttpContextWrapper,此时KoobooHttpContextWrapper要如何设计就要看我们实际的需求了。新的UrlRoutingModule创建好之后,我们需要应用它,让它生效,以取代默认的实例。打开web.config,查找:System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35替换为新的UrlRoutingModule类型名称。