理解和用好System.Web.Abstractions.dll
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的完整代码:
001 [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission (SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)]
002 public class UrlRoutingModule : IHttpModule
003 {
004 // Fields
005 private static readonly object _requestDataKey = new object();
006 private RouteCollection _routeCollection;
007
008 // Methods
009 protected virtual void Dispose()
010 {
011 }
012
013 protected virtual void Init(HttpApplication application)
014 {
015 application.PostResolveRequestCache += new EventHandler (this.OnApplicationPostResolveRequestCache);
016 application.PostMapRequestHandler += new EventHandler (this.OnApplicationPostMapRequestHandler);
017 }
018
019 private void OnApplicationPostMapRequestHandler(object sender, EventArgs e)
020 {
021 HttpContextBase context = new HttpContextWrapper (((HttpApplication) sender).Context);
022 this.PostMapRequestHandler (context);
023 }
024
025 private void OnApplicationPostResolveRequestCache(object sender, EventArgs e)
026 {
027 HttpContextBase context = new HttpContextWrapper(((HttpApplication) sender).Context);
028 this.PostResolveRequestCache(context);
029 }
030
031 public virtual void PostMapRequestHandler(HttpContextBase context)
032 {
033 RequestData data = (RequestData) context.Items[_requestDataKey];
034 if (data != null)
035 {
036 context.RewritePath(data.OriginalPath);
037 context.Handler = data.HttpHandler;
038 }
039 }
040
041 public virtual void PostResolveRequestCache (HttpContextBase context)
042 {
043 RouteData routeData = this.RouteCollection.GetRouteData(context);
044 if (routeData != null)
045 {
046 IRouteHandler routeHandler = routeData.RouteHandler;
047 if (routeHandler == null)
048 {
049 throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, RoutingResources.UrlRoutingModule_NoRouteHandler, new object[0]));
050 }
051 if (!(routeHandler is StopRoutingHandler))
052 {
053 RequestContext requestContext = new RequestContext(context, routeData);
054 IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext);
055 if (httpHandler == null)
056 {
057 throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, RoutingResources.UrlRoutingModule_NoHttpHandler, new object[] { routeHandler.GetType() }));
058 }
059 context.Items[_requestDataKey] = new RequestData { OriginalPath = context.Request.Path, HttpHandler = httpHandler };
060 context.RewritePath("~/UrlRouting.axd");
061 }
062 }
063 }
064
065 void IHttpModule.Dispose()
066 {
067 this.Dispose();
068 }
069
070 void IHttpModule.Init(HttpApplication application)
071 {
072 this.Init(application);
073 }
074
075 // Properties
076 public RouteCollection RouteCollection
077 {
078 get
079 {
080 if (this._routeCollection == null)
081 {
082 this._routeCollection = RouteTable.Routes;
083 }
084 return this._routeCollection;
085 }
086 set
087 {
088 this._routeCollection = value;
089 }
090 }
091
092 // Nested Types
093 private class RequestData
094 {
095 // Properties
096 public IHttpHandler HttpHandler { get; set; }
097
098 public string OriginalPath { get; set; }
099 }
100 }
我们可以看到,在OnApplicationPostMapRequestHandler和 OnApplicationPostResolveRequestCache ,都包含有实例化HttpContextWrapper的代码。接下来的问题就很简单了,我们只需要从 System.Web.Routing.UrlRoutingModule继承下来,实现一个新的HttpModule,重新定制这两个事件,并 且调用相同的PostMapRequestHandler和PostResolveRequestCache,传不同的 HttpContextBase对象,问 题就解决了:
01 public class KoobooUrlRoutingModule : UrlRoutingModule
02 {
03 protected override void Init(HttpApplication application)
04 {
05 application.PostResolveRequestCache += new EventHandler (this.OnApplicationPostResolveRequestCache);
06 application.PostMapRequestHandler += new EventHandler (this.OnApplicationPostMapRequestHandler);
07 }
08 private void OnApplicationPostMapRequestHandler(object sender, EventArgs e)
09 {
10 HttpContextBase context = new KoobooHttpContextWrapper(((HttpApplication) sender).Context);
11 this.PostMapRequestHandler(context);
12 }
13
14 private void OnApplicationPostResolveRequestCache(object sender, EventArgs e)
15 {
16 HttpContextBase context = new KoobooHttpContextWrapper(((HttpApplication)sender).Context);
17 this.PostResolveRequestCache(context);
18 }
19 }
HttpContextBase被替换成了KoobooHttpContextWrapper,此时 KoobooHttpContextWrapper要如何设 计就要看我们实际的需求了。新的UrlRoutingModule创建好之后,我们需要应用它,让它生效,以取代默 认的实例。打开web.config,查找:System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35替换为新的UrlRoutingModule 类型名称。