Asp. NET Pipeline Technology

Source: Internet
Author: User

ASP. NET MVC is an open-source framework that implements code separation based on the DNF. The ASP. NET MVC framework has very strong customization and extended features. This article combs the pipeline (pipeline) knowledge of ASP.


Routing is the first step of the amp (ASP. Pipleline, MVC Pipeline, hereinafter referred to as AMP). Typically, it is a matching system that is used to identify whether a request is registered in the route table.

The Urlrouteingmodule module is specifically designed to match HTTP requests from users.

When ASP. NET MVC first starts, ASP. NET MVC registers a series of URL match expressions with the route table and tells RouteTable what to do if the request is successfully matched. An application (Web application) has only one routetable and is executed only in Global.asax's Application_Start event to register a matching expression. The code is as follows:

 Public classRouteconfig { Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}); } }
protected void  //Othercode was removed for clarity  routeconfig.registerroutes (routetable.routes);}

When UrlRoutingModule finds a matching route (route) in the RouteCollection, it retrieves an instance of Iroutehandle in this route (route). The UrlRoutingModule module will then get a ihttphandler in this instance. Because the route (System.Web.Mvc.IRouteHandler) implements the following interfaces:

 Public Interface iroutehandler{IHttpHandler Gethttphandler (RequestContext requestcontext);}

This completes the routing step.

Controller initialization

When Mvchanlder is acquired, Mvchandler uses ProcessRequest to initialize the real job process inside ASP. This method uses Icontrollerfactory (the default is the System.Web.Mvc.DefaultControllerFactory) instance to create the current controller. The code is as follows:

protected Internal Virtual voidProcessRequest (HttpContextBase HttpContext) {Securityutil.processinapplicationtrust (Delegate{IController controller;     Icontrollerfactory Factory;  This. Processrequestinit (HttpContext, outController outfactory); Try{controller. Execute ( This.     RequestContext); }     finally{Factory.     Releasecontroller (Controller); }     });}
Action execution

1. When the controller initialization is complete, the controller will call the controller's own Invokeaction () method with the information of the selected action. This step is done by Iactioninvoker, with the following code:

 Public Virtual BOOL string actionname)

2. After selecting the correct action method, Model Binder (by default, System.Web.Mvc.DefaultModelBinder) retrieves the data in the HTTP request and transforms it, data validation (such as whether it is empty, The date format is correct, and so on), the input data values are also mapped to the corresponding parameters of the action method.

3. Authentication Filter (Identity authentication filter) is referenced in ASP. MVC5 and is executed before authorization filter (permission authentication). It is used to authenticate a user. Authentication filter processes The credentials of the user in the request and provides a corresponding role. Before ASP. NET MVC5, you would use authorization filter to do both identity authentication and permission authentication. By default, the Authenticate attribute is used to trigger the execution of authentication, and you can create your own authentication Filter by implementing the Iauthenticationfilter interface.

4. Authorization Filter (Permission authentication filter) allows you to authenticate a user who is already authenticated. For example, a role allows access to certain resources. By default, the authorize attribute is used to trigger execution of authorization (permissions), and you can also create your own authorization Filter by implementing the Iauthorizationfilter interface.

5. The OnActionExecuting method of the action filter can be executed before the action is executed, and onactionexecuted will be executed after the action is executed. You can define your own action Filter by implementing an interface Iactionfilter.

6. When the action is executed, the user's input is processed by the model (business model or data model) and the return result of the action is prepared.

Result execution

1. The method of result filter (onresultnexecuting) is executed before ActionResult is executed, while onresultnexecuted executes after ActionResult is executed. You can define your own result Filter by implementing an interface Iresultfilter.

2, Action result is the user's input through the business layer, the data layer and other processing results obtained. The type of Action result may be viewresult, Partialviewresult, Redirecttorouteresult, Redirectresult, Contentresult, Jsonresult, Fileresult and Emptyresult. All the result types provided by the ASP. NET MVC can be divided into two categories--viewresult (view type) and Nonviewresult (non-view type). The result that needs to be rendered and returns an HTML page is Viewresult, while the others are nonviewresult--including binary, JSON, and so on.

View initialization and rendering

1, Viewresul type (view, partial view, etc.) implements the System.Web.Mvc.IView interface, so it provides

void Render (ViewContext ViewContext, TextWriter writer);

Such a method. This method is called by the view engine.

2, the operation in this procedure is done by System.Web.Mvc.IViewEngine, by default, ASP. NET MVC provides WebForm and razor two kinds of engines. By implementing Iviewengine You can create your own engine and then register it in an ASP. NET MVC application with the following registration code:

protected void Application_Start () {  //Remove all View Engine including Webform and Razor    Register Your Custom View Engine ViewEngines.Engines.Add (new//  Other code was removed for clarity}

3, HTML helpers is used to add input, link and other HTML components (DOM elements?). )。 Html Helpers is an extension of the HtmlHelper class and can be extended in depth with ease. It makes writing form, JavaScript, and jquery easier and faster.

[Reprint Please specify the original address: Http:// Shadow Gray Butterfly]

So, thank you, the words of light mistakenly spray, the wrong place also please correct me, welcome to discuss.

Asp. NET Pipeline Technology

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: 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.