You are on page 1of 5

6/27/2014

Asp.net MVC Request Life Cycle

Asp.net MVC Request Life Cycle


P o st e d By : S h aile ndra Ch auh an, 0 4 N o v 2 0 1 2
U pdat e d On : 1 1 Jun 2 0 1 4
V e rs i o n Su p p o rt : MV C5 , MV C4 & MV C3

Ke y wo rd s : a s p .n e t mv c p i p e l i n e ,mv c3 re q u e s t l i fe cy cl e ,a s p .n e t mv c4 p a g e l i fe
cy cl e ,a s p .n e t mv c a p p l i ca t i o n p a g e l i fe cy cl e

hile programming with Asp.net MVC, you should be aware of the life of an Asp.net MVC request
from birth to death. In this article, I am going to expose the Asp.net MVC Request Life cycle. There

are seven main steps that happen when you make a request to an Asp.net MVC web applications. For
more details refer Detailed ASP.NET MVC Pipeline

01. Routing
Asp.net Routing is the first step in MVC request cycle. Basically it is a pattern matching system that
matches the requests 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
IRouteHandler for that request. The default one calls the MvcHandler. The routing engine returns a
404 HTTP status code against that request if the patterns is not found in the Route Table.

http://www.dotnet-tricks.com/Tutorial/mvc/TbR0041112-Asp.net-MVC-Request-Life-Cycle.html

1/5

6/27/2014

Asp.net MVC Request Life Cycle

When application starts at first time, it registers one or more patterns to the Route Table to tell the
routing system what to do with any requests that match these patterns. An application has only one
Route Table and this is setup in the Global.asax file of the application.
1.

public static void RegisterRoutes(RouteCollection routes)

2.

3. routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
4.

routes.MapRoute( "Default", // Route name

5.

"{controller}/{action}/{id}", // URL with parameters

6.

new { controller = "Home", action = "Index", id = UrlParameter.Optional


} // Parameter defaults

7.

);

8. }

02. MvcHandler
The MvcHandler is responsible for initiating the real processing inside ASP.NET MVC. MVC handler
implements IHttpHandler interface and further process the request by using ProcessRequest
http://www.dotnet-tricks.com/Tutorial/mvc/TbR0041112-Asp.net-MVC-Request-Life-Cycle.html

2/5

6/27/2014

Asp.net MVC Request Life Cycle

method as shown below:


1. protected internal virtual void ProcessRequest(HttpContextBase
httpContext)
2. {
3.

SecurityUtil.ProcessInApplicationTrust(delegate {

4.

IController controller;

5.

IControllerFactory factory;

6.

this.ProcessRequestInit(httpContext, out controller, out factory);

7.

try

8.

9.

controller.Execute(this.RequestContext);

10.

11.

finally

12.

13.

factory.ReleaseController(controller);

14.

15.

});

16. }

03. 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 be the default
controller factory or a custom factory initialized at the Application_Start event, as shown below:
1. protected void Application_Start()
2. {
3.

AreaRegistration.RegisterAllAreas();

4.

RegisterRoutes(RouteTable.Routes);

5.

ControllerBuilder.Current.SetControllerFactory(new
CustomControllerFactory());

6. }

04. Action Execution


Once the controller has been instantiated, Controller's ActionInvoker determines which specific action
to

invoke

on

the

controller.

Action

to

be

execute

is

chosen

based

on

attributes

ActionNameSelectorAttribute (by default method which have the same name as the action is
chosen) and ActionMethodSelectorAttribute(If more than one method found, the correct one
is chosen with the help of this attribute).

05. View Result


http://www.dotnet-tricks.com/Tutorial/mvc/TbR0041112-Asp.net-MVC-Request-Life-Cycle.html

3/5

6/27/2014

Asp.net MVC Request Life Cycle

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

06. View Engine


The first step in the execution of the View Result involves the selection of the appropriate View Engine
to render the View Result. It is handled by IViewEngine interface of the view engine. By default
Asp.Net MVC uses WebForm and Razor view engines. You can also register your own custom view
engine to your Asp.Net MVC application as shown below:
1. protected void Application_Start()
2. {
3.

//Remove All View Engine including Webform and Razor

4.

ViewEngines.Engines.Clear();

5.

//Register Your Custom View Engine

6.

ViewEngines.Engines.Add(new CustomViewEngine());

7.

//Other code is removed for clarity

8. }

07. 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 an HTML page to the browser by using the
current view engine.

What do you think?


I hope you will enjoy the Asp.Net MVC request life cycle while programming with Asp.Net MVC. I would
like to have feedback from my blog readers. Your valuable feedback, question, or comments about this
article are always welcome.
Print Article

Share this article with your friends!


in S h a r e 1 3

Tw eet

About the Author

http://www.dotnet-tricks.com/Tutorial/mvc/TbR0041112-Asp.net-MVC-Request-Life-Cycle.html

4/5

6/27/2014

Asp.net MVC Request Life Cycle

Shailendra Chauhan works as Software Analyst at reputed MNC and has more than 5 years of hand
over Microsoft .NET technologies. He is a .NET Consultant and is the founder & chief editor of
www.dotnet-tricks.com and www.dotnetinterviewtricks.com blogs. He is an author of book ASP.NET
MVC Interview Questions and Answers.
He loves to work with web applications and mobile apps using Microsoft technology including ASP.NET,
MVC, C#, SQL Server, WCF, Web API, Entity Framework,Cloud Computing, Windows Azure, jQuery,
jQuery Mobile, Knockout.js, Angular.js and many more web technologies. More...

http://www.dotnet-tricks.com/Tutorial/mvc/TbR0041112-Asp.net-MVC-Request-Life-Cycle.html

5/5

You might also like