What is the AspNet mvc?

Source: Internet
Author: User
Tags httpcontext

ASP.net is a development framework for building Web pages and Web sites through HTML, CSS, JavaScript, and server scripting.

MVC is one of three asp.net development models.

MVC is a framework for building Web applications, using the MVC (Model View Controller) Design:

Model represents the core of an application (such as a list of database records)

View (views) to display data (database records)

Controller (Controller) process input (write to database records)

The MVC model also provides complete control over HTML, CSS, and JavaScript.

The MVC model defines a Web application with three logical tiers:

Business layer (business layer, model logic)

Display layer (show layer, view logic)

Input control (Enter controls, controller logic)

The more important context in the AspNet MVC is as follows:

The context of the core has HttpContext (Request context), ControllerContext (Controller context)

The filter has five contextual actionexecutingcontext,actionexecutedcontext,resultexecutingcontext,resultexecutedcontext, Exceptioncontext

View-related context ViewContext

The relationships between these contexts are shown in the following illustration

Description

1, ControllerContext is the package of HttpContext

2, filters and other filtercontext contexts are inherited from ControllerContext

3, ViewContext is also inherited from ControllerContext, while encapsulating the object of the view

From this we can see that the most basic or the HttpContext context of the ASPNET runs through the entire request/response, while MVC HttpContext is encapsulated into ControllerContext again. So look at the context of HttpContext and ControllerContext to get a general idea of these contexts.

1, the Origin of HttpContext

First look at a 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 and internally implements the creation of AppDomain "HttpRuntime, HttpContext and so on 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 Isapiruntime ProcessRequest obtained in the previous step. During

① must wrap workrequest for different versions of IIS to create a Isapiworkerrequest instance object

②httpruntime invokes Processrequestnodemand to process the workrequest that is obtained above, 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 *);

HttpRequest and HttpResponse are also initialized within the ③httpcontext constructor

Specific internal details, you can poke around 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 post controller. ControllerContext will also serve 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 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 enables 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 the iactionfilter containing the actionexecutingcontext,actionexecutedcontext context, and the internal controlleractioninvoker of the invokeactionmethodwithfilters 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 rendering views

As a result, the contextual content inside MVC is basically introduced. 、

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.