0 Preface
The more important contexts in AspNet MVC are as follows:
- The context of the core has HttpContext (Request context), ControllerContext (Controller context)
- The filter has five context Actionexecutingcontext,actionexecutedcontext,resultexecutingcontext,resultexecutedcontext, Exceptioncontext
- View-related context ViewContext
The relationships between these contexts are as follows
Description
1, ControllerContext is the package of HttpContext
2, filters and other filtercontext contexts are inherited from the ControllerContext
3, ViewContext is also inherited from the ControllerContext, while encapsulating the object of the view
As can be seen, the most basic or ASPNET HttpContext context throughout the request/response, and MVC is to HttpContext again encapsulated into ControllerContext. So we can understand the context of HttpContext and ControllerContext first.
1 The origin of HttpContext
First look at the picture of the uncle in the garden, as shown below.
The approximate process is as follows
- The Appmanagerappdomainfactory class implements the Create method of the Iappmanagerappdomainfactory interface, which internally implements the creation of the AppDomain "HttpRuntime, HttpContext, etc. are attached to the AppDomain ", hostingenvironment and so on a series of operations, and get a isapiruntime.
- When IIS accepts a request, it can process the request through the ProcessRequest of the Isapiruntime obtained in the previous step. During
① must wrap workrequest for different versions of IIS to create Isapiworkerrequest instance objects
②httpruntime calls Processrequestnodemand to handle the resulting workrequest, and the context of the request is instantiated by ProcessRequestInternal, as shown in the following code
1 |
HttpContext context = new HttpContext(wr/WorkRequest*/, false /* initResponseWriter */ ); |
The ③httpcontext constructor also initializes the HttpRequest and HttpResponse
Specific internal details can be poked here to see the uncle in-depth analysis
2 ControllerContext
ControllerContext is instantiated inside the Controllerbase Initialize method, controllerbase as the base class, inherited by the later controller. ControllerContext will also act as the base class for other filter contexts.
12345678910111213141516171819 |
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 Filter Context
The filter uses AOP (aspect-oriented programming), which allows for additional filtering effects by implementing the Iactionfilter,iresultfilter,iexceptionfilter,iauthorizationfilter interface. The parameters of the internal methods of these interfaces have corresponding contexts, such as iactionfilter internal containing actionexecutingcontext,actionexecutedcontext contexts, And the invokeactionmethodwithfilters inside the Controlleractioninvoker is instantiated.
1234567891011 |
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 View Context
The view context is instantiated in three places: Viewresultbase,httphelper, Templatehelpers, which provides more data support for the rendered view. Take the example of Viewresultbase (inheriting ActionResult and overriding viewcontext within the Executeresult method), as follows
1234567891011121314151617181920212223 |
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);
}
}
|
To this, the basic content of the internal context of MVC is introduced. If you feel good, please praise the next, the wrong words please point out, thank you!
Original address: http://www.cnblogs.com/luge/p/Mvc_Context.html
MVC Learning Notes---various contextual contexts