Various contextual understandings in AspNet MVC

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

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.

protected virtual void Initialize (RequestContext requestcontext) {            controllercontext = new ControllerContext ( RequestContext, this);        } Public RequestContext RequestContext {            get {                if (_requestcontext = = null) {                    //still need explicit calls to Co Nstructors since the property getters is virtual and might return null                    httpcontextbase httpcontext = HttpContext?? n EW 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.

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)      ;  Omitted    }

4 View Context

The view context is instantiated in three places: Viewresultbase,httphelper, Templatehelpers, which provides more data support for the rendered 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!

Various contextual understandings in AspNet MVC

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.