0 前言html
AspNet MVC中比較重要的上下文,有以下:編程
- 核心的上下文有HttpContext(請求上下文),ControllerContext(控制器上下文)
- 過濾器有關有五個的上下文ActionExecutingContext,ActionExecutedContext,ResultExecutingContext,ResultExecutedContext,ExceptionContext
- 視圖相關的上下文ViewContext
這些上下文之間的關係以下圖所示ide
說明:函數
一、ControllerContext是對HttpContext的封裝post
二、過濾器等filterContext上下文都是繼承自ControllerContextui
三、ViewContext也是繼承自ControllerContext,同時封裝了對視圖的對象this
由此能夠看出,最基礎仍是Aspnet的HttpContext上下文貫穿整個請求/應答的,而Mvc是將HttpContext進行再次封裝成ControllerContext。因此先看明白HttpContext與ControllerContext的前因後果便可大體瞭解這些上下文。spa
1 HttpContext的由來code
先看看園裏大叔的一張圖,以下所示。htm
大體的流程以下
- AppManagerAppDomainFactory類實現IAppManagerAppDomainFactory接口的Create方法,內部實現了建立AppDomain【HttpRuntime、HttpContext等都依附在AppDomain】、HostingEnvironment等一系列操做,而且獲得一個ISAPIRuntime。
- 當IIS接受一個請求就能夠經過上一步所獲得的ISAPIRuntime的ProcessRequest進行處理請求。其間
①必須對WorkRequest針對不一樣的IIS版本進行包裝,從而建立獲得ISAPIWorkerRequest實例對象
②HttpRuntime調用ProcessRequestNoDemand處理上面所獲得的WorkRequest,而且經過ProcessRequestInternal 實例化化請求的上下文,以下代碼所示
1
|
HttpContext context =
new
HttpContext(wr/WorkRequest*/,
false
/* initResponseWriter */
);
|
③HttpContext的構造函數內部也初始化HttpRequest以及HttpResponse
具體的內部細節,能夠猛戳這裏去看大叔深刻剖析
2 ControllerContext
ControllerContext在ControllerBase的Initialize方法內部被實例化,ControllerBase做爲基類,被後期控制器所繼承。ControllerContext也將做爲其餘的過濾器上下文的基類。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
protected
virtual
void
Initialize(RequestContext requestContext) {
ControllerContext =
new
ControllerContext(requestContext,
this
);
}
public
RequestContext RequestContext {
get
{
if
(_requestContext ==
null
) {
// still need explicit calls to constructors since the property getters are virtual and might return null
HttpContextBase httpContext = HttpContext ??
new
EmptyHttpContext();
RouteData routeData = RouteData ??
new
RouteData();
_requestContext =
new
RequestContext(httpContext, routeData);
}
return
_requestContext;
}
set
{
_requestContext = value;
}
}
|
3 過濾器上下文
過濾器採用AOP(面向切面編程),能夠經過實現IActionFilter,IResultFilter,IExceptionFilter,IAuthorizationFilter接口,進行附加的過濾效果。這些接口的內部方法的參數有相對應的上下文,如IActionFilter內部含有ActionExecutingContext,ActionExecutedContext上下文,並且將ControllerActionInvoker的InvokeActionMethodWithFilters內部被實例化
1
2
3
4
5
6
7
8
9
10
11
|
public
interface
IActionFilter {
void
OnActionExecuting(ActionExecutingContext filterContext);
void
OnActionExecuted(ActionExecutedContext filterContext);
}
protected
virtual
ActionExecutedContext InvokeActionMethodWithFilters(ControllerContext controllerContext, IList<IActionFilter> filters, ActionDescriptor actionDescriptor, IDictionary<
string
,
object
> parameters) {
ActionExecutingContext preContext =
new
ActionExecutingContext(controllerContext, actionDescriptor, parameters);
//省略
}
|
4 視圖上下文
視圖上下文被實例化三個地方:ViewResultBase,HttpHelper、TemplateHelpers,該上下文更多的爲渲染視圖提供數據支持。以ViewResultBase(繼承ActionResult並重寫ExecuteResult方法內部對ViewContext進行實例化)爲例,以下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public
override
void
ExecuteResult(ControllerContext context) {
if
(context ==
null
) {
throw
new
ArgumentNullException(
"context"
);
}
if
(String.IsNullOrEmpty(ViewName)) {
ViewName = context.RouteData.GetRequiredString(
"action"
);
}
ViewEngineResult result =
null
;
if
(View ==
null
) {
result = FindView(context);
View = result.View;
}
TextWriter writer = context.HttpContext.Response.Output;
ViewContext viewContext =
new
ViewContext(context, View, ViewData, TempData, writer);
View.Render(viewContext, writer);
if
(result !=
null
) {
result.ViewEngine.ReleaseView(context, View);
}
}
|
致此,基本介紹了MVC內部的上下文內容。若是以爲不錯請點贊下,有誤的話請指出,謝謝!