先来个图预览一下:
![](https://www.cnblogs.com/images/cnblogs_com/wind-eagle/117889/o_MVC_Context.jpg)
1.首先是System.Web.Routing下的RequestContext类,代码相当简单,把HttpContextBase和RouteData放在一起就OK了。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
using System;
using System.Security.Permissions;
using System.Web;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace System.Web.Routing
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class RequestContext
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Methods
public RequestContext(HttpContextBase httpContext, RouteData routeData)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (httpContext == null)
throw new ArgumentNullException("httpContext");
if (routeData == null)
throw new ArgumentNullException("routeData");
this.HttpContext = httpContext;
this.RouteData = routeData;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Properties
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public HttpContextBase HttpContext
{ get; internal set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public RouteData RouteData
{ get; internal set; }
}
}
关于RouteData,我以后会讲的,现在只需要知道它存储了Route信息,从URL中根据Route规则Map到的参数值都在里面。
2.接着就是ControllerContext类了,这个类很有意思:
using System.Security.Permissions;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace System.Web.Mvc
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
using System.Web.Routing;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class ControllerContext : RequestContext
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
protected internal ControllerContext(ControllerContext controllerContext)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
: this(GetControllerContext(controllerContext), GetControllerContext(controllerContext).Controller)
{}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public ControllerContext(HttpContextBase httpContext, RouteData routeData, ControllerBase controller)
: base(httpContext, routeData)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (controller == null) throw new ArgumentNullException("controller");
Controller = controller;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public ControllerContext(RequestContext requestContext, ControllerBase controller)
: this(
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
GetRequestContext(requestContext).HttpContext, GetRequestContext(requestContext).RouteData, controller)
{}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public ControllerBase Controller
{ get; private set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
internal static RequestContext GetRequestContext(RequestContext requestContext)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (requestContext == null) throw new ArgumentNullException("requestContext");
return requestContext;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
internal static ControllerContext GetControllerContext(ControllerContext controllerContext)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (controllerContext == null) throw new ArgumentNullException("controllerContext");
return controllerContext;
}
}
}
其实它也就是在RequestContext的基础上加了个对Controller的引用(当然是对ControllerBase基类的引用)。
它的几个重载的构造函数以及静态方法写得很有意思,值得借鉴。
3.