Walk through the ASP. net mvc processing pipeline, asp. netmvc
ASP. net mvc has been around for several years since its birth. This framework provides a new development model that is more in line with the nature of web development. You can use and personalize and expand this framework well, but you need to know enough about it. This article mainly summarizes the MVC processing model from the overall perspective.
Put a picture of the overall processing model first (the figure is most intuitive and persuasive): ASP. NETMVC5Pipeline describes the following parts: routing module 1. in ASP. the first stop in the net mvc processing pipeline is the routing module. When a request arrives at the routing module, the MVC Framework matches the current request according to the routing template configured in the Route Table to obtain the corresponding controller and action information. The specific matching process is implemented by using UrlRoutingModule (System. Web. Routing. UrlRoutingModule. 2. when ASP. when the net mvc application is started for the first time, the routing system will add the routing rules we registered (which requests are intercepted) to the Route Table. An application contains a Route Table in the Global. the Application_Start event in asax is created: copy the code public class RouteConfig {public static void RegisterRoutes (RouteCollection routes) {routes. ignoreRoute ("{resource }. axd/{* pathInfo} "); routes. mapRoute (name: "Default", url: "{controller}/{action}/{id}", defaults: new {controller = "Home", action = "Index ", id = Url Parameter. optional}) ;}} protected void Application_Start () {RouteConfig. registerRoutes (RouteTable. routes);} copy code 3. when the UrlRoutingModule finds a matched routing rule in the Route Table, it will find the corresponding IRouteHandler (System. web. mvc. IRouteHandler) instance (default is System. web. mvcRouteHandler), obtain an IHttpHandler instance based on this RouteHandler (default: System. web. mvcHandler) public interface IRouteHandler {IHttpHandler GetHttpHandler (RequestContext requestCon Text);} Controller initialization 1. the ProcessRequest method in MvcHandler is ASP. net mvc life cycle, This method uses the IControllerFactory instance (default is System. web. mvc. defaultControllerFactory) to create the corresponding controller: copy the Code protected internal virtual void ProcessRequest (HttpContextBase httpContext) {SecurityUtil. processInApplicationTrust (delegate {IController controller; IControllerFactory factory; this. processRequestInit (httpContext, out controller, out Factory); try {controller. execute (this. requestContext);} finally {factory. releaseController (controller) ;}}) ;}copy the code Action execution 1. after the controller is created, it will then execute its own InvokeAction () method: public virtual bool InvokeAction (ControllerContext controllerContext, string actionName) 2. after the appropriate action is selected, model binders (default: System. web. mvc. defaultModelBinder), which extracts data from parameters of the http request and implements type conversion, data validation (for example, whether required or not, data format, etc.), and whether it is automatically assembled into parameters of the action method. System. Web. Mvc. DefaultModelBinder 3. Authentication Filter is a newly added Filter in mvc5. It is executed before the authorization filter to authenticate users. Before MVC5, authentication and authorization were implemented through the authorization filter, But now these two operations are separated and managed separately. 4. Action filters has two methods: OnActionExecuting and OnActionExecuted, which are executed before and after the action is executed. We can also implement your personalized filtering mechanism by implementing the IActionFilter interface. 5. the next step is to execute the code we usually write in the action method (based on the corresponding results of the request) The execution of ActionResult 1. before and after the ActionResult is executed, there will still be a filter (ResultFilter). Similarly, you can customize your own filter logic by implementing the IResultFilter interface. 2. ActionResult is to return the user request result processed by bal dal. Therefore, ViewResult, PartialViewResult, RedirectToRouteResult, RedirectResult, ContentResult, JsonResult, FileResult and EmptyResult are specific return types. The preceding return types can be roughly divided into two categories: ViewResult and non-ViewResult. If you want to generate an html page and insert ViewResult to the client, other elements such as returned text and json data are classified into non-ViewResult. You can directly return non-ViewResult. Initialization and rendering of View 1. for ViewResult, the appropriate View Engine calls the IView Render () method to Render the View: public interface IView {void Render (ViewContext viewContext, TextWriter writer);} 2. the entire process is handled by IViewEngine (System. web. mvc. IViewEngine. ASP. net mvc provides webform (. aspx) and Razor (. cshtml) template engine, you can implement your own ViewEngine by implementing the IViewEngine interface, and then register in the Application_Start method as follows: copy the Code protected void Application_Start () {// remove all View engines, including Webform and Razor ViewEngines. engines. clear (); // register your own View engine ViewEngines. engines. add (new CustomViewEngine ();} copy code 3. finally, Html Helpers will help us generate input tags, AJAX-based forms, and so on. Html Helpers is an extension method of the HtmlHelper class, so it is very easy to extend it further.