ASP. NET MVC Request Life Cycle
While programming with ASP. NET MVC, you should is aware of the life's an ASP. NET MVC request from birth to death. In this article, I am going to expose the ASP Request life cycle. There is seven main steps that happen if you do a request to an ASP. Web applications. For more details refer detailed ASP. NET MVC Pipeline
- Routing
ASP. NET Routing is the first step in MVC request cycle. Basically it is a pattern matching system that matches the request's URL against the registered URL patterns in the Route Table. When a matching pattern found in the Route Table, the Routing engine forwards the request to the corresponding Iroutehandl Er for that request. The default one calls the MvcHandler
. The routing engine returns a 404 HTTP status code against that request if the patterns are not found in the Route Table.
When application starts at first time, it registers one or more patterns to the Route Table to tell the routing system WHA T to does with any requests this match these patterns. An application have only one Route Table and this is setup in the Global.asax file of the application.
- public static void registerroutes(routecollection routes)
- {
- Routes. Ignoreroute("{resource}.axd/{*pathinfo}");
- Routes. MapRoute( "Default", //Route name
- ' {controller}/{action}/{id} ', //URL with parameters
- new { Controller = "Home", action = "Index", id = urlparameter. Optional } //Parameter defaults
- );
- }
- Mvchandler
The Mvchandler is responsible for initiating the real processing inside ASP. MVC handler implements IHttpHandler interface and further process the request by using ProcessRequest
method as shown below:
- 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);
- }
- });
- }
- Controller
As shown in above code, Mvchandler uses the Icontrollerfactory instance and tries to get a IController instance. If successful, the Execute method is called. The icontrollerfactory could is the default controller factory or a custom factory initialized Application_Start
at the event, as show N below:
- protected void Application_Start()
- {
- arearegistration. Registerallareas();
- registerroutes(routetable. Routes);
- controllerbuilder. Current. Setcontrollerfactory(new customcontrollerfactory());
- }
- Action execution
Once the controller has been instantiated, controller ' s actioninvoker determines which specific action-invoke on the CO Ntroller. Action to was execute is chosen based on attributes ActionNameSelectorAttribute
(by default method which has the same name as the action is Chos EN) ActionMethodSelectorAttribute
and (If more than one method found, the correct one are chosen with the "this attribute").
- View Result
The action method receives user input, prepares the appropriate response data, and then executes the result by returning a Result type. The result type can be ViewResult, Redirecttorouteresult, Redirectresult, Contentresult, Jsonresult, Fileresult, and Empty Result.
- View Engine
The first step in the execution of the view Result involves the selection of the appropriate View Engine to render the Vie W Result. It is handled by IViewEngine
interface of the view engine. By default ASP. NET MVC uses and WebForm
Razor
view engines. You can also register your own custom view engine to your ASP application as shown below:
- protected void Application_Start()
- {
- //remove all View Engine including Webform and Razor
- viewengines. Engines. Clear();
- //register Your Custom View Engine
- viewengines. Engines. Add(new customviewengine());
- //other code is removed for clarity
- }
- View
Action method May returns a text string,a binary file or a Json formatted data. The most important Action Result is the ViewResult, which renders and returns a HTML page to the browser by using the cur Rent view engine.
What does you think?
I hope you'll enjoy the ASP. NET MVC request life cycle while programming with ASP. I would like to has feedback from my blog readers. Your valuable feedback, question, or comments about this article is always welcome.
ASP. NET MVC Request Life Cycle