MVC Learning Notes---various contextual contexts

Source: Internet
Author: User

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 = newHttpContext(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 protectedvirtualvoidInitialize(RequestContext requestContext) {            ControllerContext = new ControllerContext(requestContext, this);        }publicRequestContext RequestContext {            get{                if (_requestContext == null) {                    // still need explicit calls to constructors since the property getters are virtual and might return null                    HttpContextBase httpContext = HttpContext ?? newEmptyHttpContext();                    RouteData routeData = RouteData ?? newRouteData();                    _requestContext = newRequestContext(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 publicinterfaceIActionFilter {        voidOnActionExecuting(ActionExecutingContext filterContext);        voidOnActionExecuted(ActionExecutedContext filterContext);    }protectedvirtualActionExecutedContext InvokeActionMethodWithFilters(ControllerContext controllerContext, IList<IActionFilter> filters, ActionDescriptor actionDescriptor, IDictionary<stringobject> parameters) {            ActionExecutingContext preContext = newActionExecutingContext(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 publicoverridevoidExecuteResult(ControllerContext context) {          if(context == null) {              thrownewArgumentNullException("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 = newViewContext(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.