ASP. NET MVC Deep Contact: ASP. NET MVC request life cycle

Source: Internet
Author: User

The purpose of this blog post is to describe in detail every process of ASP. NET MVC request from start to finish. I want to understand anything that happens after the browser enters a URL and taps enter to request a page for an ASP. NET MVC site.

Why do you need to care about this? There are two reasons. The first is that ASP. NET MVC is a very extensible framework. For example, we can insert different viewengine to control how the site content is rendered. We can also define how the controller generates and assigns to a request. Because I want to explore any extension points for the ASP. NET MVC page request, I'm going to explore some of the steps in the request process.

Second, I am interested in test-driven development like Blake. To write unit tests for a controller, we have to understand the controller's dependencies. When writing tests, I needed to emulate some objects using mock frames such as Typemock isolator or Rhino Mocks. If you do not understand the page request life cycle, you cannot perform a valid simulation.

Two warnings

First, we need to give you two warnings.

First warning: I wrote this article after ASP. NET MVC Preview2 was released externally. The ASP. NET MVC Framework will change a lot before beta. Therefore, anything I describe in this article will not be valid for a few months. So, if you read this article after May 2008, don't trust anything you read.

Second, this article is not an overview of ASP. I'm just a hay. Describes the life cycle of an ASP. NET MVC request. Well, don't say I didn't warn you.

Overview of life cycle steps

When we make a request to the ASP. NET MVC site, there are 5 main steps:

1, step to create routetable

The first step occurs when the ASP. NET application starts for the first time. RouteTable maps the URL to handler.

2. Step 2--urlroutingmodule Intercept Request

The second step occurs when we initiate the request. UrlRoutingModule intercepts each request and creates and executes the appropriate handler.

3, step 3--execution Mvchandler

Mvchandler creates the controller and passes the controller in to ControllerContext, then executes the controller.

4. Step 4--Execution Controller

The controller detects the Controller method to be executed, constructs the parameter list, and executes the method.

5. Step 5--Call the Renderview method

In most cases, the Controller method calls Renderview () to render the content back to the browser. The Controller.renderview () method delegates the work to a viewengine.

Let's look at each step in detail.

Steps to create routetable

When we request a normal ASP. NET Application page, there is a page on the disk for each page request. For example, if we request a page called somepage.aspx, there will be a page called somepage.aspx on the Web server. If not, you will get an error.

Technically speaking, an ASP. NET page represents a class and is not a normal class. The ASP. NET page is a handler. In other words, the ASP. NET page implements the IHttpHandler interface and has a ProcessRequest () method to accept the request when the page is requested. The ProcessRequest () method is responsible for generating the content and sending it back to the browser.

As a result, common ASP. NET applications work like Blake simple and straightforward. We request the page, the page request corresponds to a page on the disk, this page executes the ProcessRequest () method and sends the content back to the browser.

The ASP. NET MVC application does not work this way. When we request a page for an ASP. NET MVC application, there is no page on the disk that corresponds to the request. Instead, the request is routed to a class called a controller. The controller is responsible for generating the content and sending it back to the browser.

When we write a normal ASP. NET application, we create a lot of pages. Mapping between URLs and pages always corresponds to one by one. Each page request corresponds to the corresponding page.

Instead, when we create an ASP. NET MVC application, we create a batch of controllers. The advantage of using a controller is that you can have many-to-one mappings between URLs and pages. For example, all of the following URLs can be mapped to the same controller.

Http://MySite/Products/1

Http://MySite/Products/2

Http://MySite/Products/3

These URLs map to a controller and display the correct product by extracting the product ID from the URL. This controller approach is more flexible than the traditional ASP. Controller mode can be a more obvious URL for the product.

So, how is a page request routed to a controller? An ASP. NET MVC application has something called a route table. The routing table maps a URL to a controller.

An application has one and only one routing table. The routing table is created in the Global.asax file. Listing 1 contains the default Global.asax file when you use Visual Studio to create a new ASP. NET MVC Web application.

List 1–global.asax

Using System;
Using System.Collections.Generic;3:Using System.Linq;4:Using System.Web;5:Using SYSTEM.WEB.MVC;6:Using System.Web.Routing;7:8:Namespace Testmvcarch9: {10:PublicClass GlobalApplication:System.Web.HttpApplication11: {12:PublicStaticvoid RegisterRoutes (RouteCollection routes)13: {14:Note:change the URL to ' {controller}.mvc/{action}/{id} ' to enable15:Automatic support on IIS6 and IIS7 Classic mode16:17:routes. ADD (New Route ("{Controller}/{action}/{id}",New Mvcroutehandler ())18: {19:defaults =New RouteValueDictionary (New {action ="Index", id ="" }),20:});21st:22:routes. ADD (New Route ( "default.aspx", new Mvcroutehandler ()) 23: {24:defaults = new RouteValueDictionary (new {controller = "Home", action = "Index", id = 25:}); 26:} 27: 28: protected void Application_Start (object sender, EventArgs e) 29: {30:registerroutes (routetable.routes); 31:} 32:} 33:}

ASP. NET MVC Deep Touch: ASP. NET MVC Request Lifecycle

Related Article

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.