ASP. NET MVC Request Life Cycle

Source: Internet
Author: User

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

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

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

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

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

    1. protected void Application_Start()
    2. {
    3. arearegistration. Registerallareas();
    4. registerroutes(routetable. Routes);
    5. controllerbuilder. Current. Setcontrollerfactory(new customcontrollerfactory());
    6. }
  4. 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").

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

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

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

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: info-contact@alibabacloud.com 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.