How to analyze the Life cycle of ASP.NET MVC
How to analyze the ASP.NET MVC life cycle, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
I am still very interested in the ASP.NET MVC lifecycle, so I ask two questions:
When a HTTP request is handed over from IIS to ASP.NET runtime, when does ASP.NETMVC gain control and process the request? What is the process of processing?
Take the ASP.NET MVC lifecycle in IIS7 as an example, a diagram of events occurring during HTTP request processing from MSDN, followed by a complete list of events. Since ASP.NET MVC is still based on the ASP.NET runtime, it is bound to intercept requests during the ASP.NET MVC life cycle. * the response is, of course, to go to web.config to look it up. We can see the configuration section of UrlRoutingModule:
See here our * question actually has the answer: the timing is in PostResolveRequestCache and PostMapRequestHandler.
ResolveRequestCache event
Occurs when ASP.NET finishes an authorization event to let the caching modules serve requests from the cache, bypassing execution of the event handler (for example, a page or an XML Web service).
Source document >
PostMapRequestHandler event
Occurs when ASP.NET has mapped the current request to the appropriate event handler.
Source document
We use the ASP.NET Mvc template in VS2008 to create a Demo for the rest of the discussion. What happens when we visit / Home?
1.Request request arrives
2.IIS transfers processing rights to ASP.NET according to the characteristics of the request.
3.UrlRoutingModule matches the current request in Route Table
4.UrlRoutingModule looks for Request matching RouteHandler in RouteCollection. By default, MvcRouteHandler MvcRouteHandler creates a MvcHandler instance.
5.MvcHandler executes ProcessRequest.
6.MvcHandler uses IControllerFactory to obtain an example that implements the IController interface and finds the corresponding HomeController
7. The Index method of triggering HomeController according to Request
8.Index stores the execution result in ViewData
The Index method of 9.HomeController returns ActionResult
10.Views/Home/Index.aspx renders ViewData on the page
11.Index.aspx executes the ProcessRequest method
12.Index.aspx executes the Render method output to the client
By reading the source code of ASP.NET Mvc, we can get more detailed processing process. I ignore the details as much as possible and emphasize the process of request processing. We cut in from the Global.asax.cs file.
After reading the above, have you mastered how to analyze the life cycle of ASP.NET MVC? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!