What is AspNet MVC?
ASP. NET is a development framework used to build Web pages and websites through HTML, CSS, JavaScript, and server scripts.
MVC is one of the three ASP. NET development models.
MVC is a framework used to build web applications. It is designed using MVC (Model View Controller:
Model indicates the application CORE (such as the database record list)
View (View) displays data (database records)
Controller processes input (written to database records)
The MVC model also provides complete control over HTML, CSS, and JavaScript.
The MVC model defines web applications through three logical layers:
Business layer (business layer and model logic)
Display layer (display layer and view logic)
Input control (input control, controller logic)
The important contexts in AspNet MVC are as follows:
The core contexts include HttpContext and ControllerContext)
The filters have five contexts: ActionExecutingContext, ActionExecutedContext, ResultExecutingContext, ResultExecutedContext, and predictioncontext.
View-related context ViewContext
Shows the relationship between these contexts.
Note:
1. ControllerContext is the encapsulation of HttpContext
2. filters and other filterContext context are inherited from ControllerContext
3. ViewContext also inherits from ControllerContext and encapsulates the view object.
It can be seen that the HTTP context of Aspnet runs through the whole request/response, while Mvc encapsulates HttpContext into ControllerContext again. So let's take a look at the ins and outs of HttpContext and ControllerContext to get a general idea of these contexts.
1. Origin of HttpContext
Let's take a look at Uncle Yuan's figure, as shown below.
The general process is as follows:
The AppManagerAppDomainFactory class implements the Create method of the IAppManagerAppDomainFactory interface, internally implements a series of operations such as creating AppDomain [HttpRuntime, HttpContext, etc. all attached to AppDomain] And HostingEnvironment, and obtains an ISAPIRuntime.
When IIS accepts a request, it can process the request through the ProcessRequest of ISAPIRuntime obtained in the previous step. In the meantime
① WorkRequest must be packaged for different IIS versions to create an ISAPIWorkerRequest instance object.
② HttpRuntime call ProcessRequestNoDemand to process the preceding WorkRequest and instantiate the request context through ProcessRequestInternal, as shown in the following code:
1. HttpContext context = new HttpContext (wr/WorkRequest */, false/* initResponseWriter */);
③ The HTTP context constructor also initializes HttpRequest and HttpResponse internally.
For specific internal details, click here to go to Uncle's in-depth analysis.
2. ControllerContext
ControllerContext is instantiated in the Initialize method of ControllerBase. ControllerBase is used as the base class and inherited by later controllers. 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 {
Get {
If (_ requestContext = null ){
// Still need explicit cballs 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 adopts AOP (Aspect-Oriented Programming). You can implement the IActionFilter, IResultFilter, IExceptionFilter, and IAuthorizationFilter interfaces to apply additional filtering effects. Parameters of the internal methods of these interfaces have the corresponding context. For example, IActionFilter contains the ActionExecutingContext and ActionExecutedContext, And the InvokeActionMethodWithFilters of ControllerActionInvoker is instantiated internally.
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, and TemplateHelpers. This context provides more data support for the rendering view.
This section describes the context of MVC. ,