Requests to an ASP.NET MVC-based Web application first pass through the UrlRoutingModule object, which is an HTTP module. This module parses the request and performs route selection. The UrlRoutingModule object selects the first route object that matches the current request. (A route object is a class that implements RouteBase, and is typically an instance of the Route class.) If no routes match, the UrlRoutingModule object does nothing and lets the request fall back to the regular ASP.NET or IIS request processing.
当请求基于ASP.NET MVC的应用程序时首先经过UrlRoutingModule的对象,它是一个Http模块。这个模块解析请求并根据路由选择执行。UrlRoutingModule对象先与第一个route对象进行匹配(route对象继承自RouteBase类的典型的Route类的实例。)如果没有匹配成功,那么UrlRoutingModule什么也不做,将控制权交给ASP.NET或IIS继续处理请求。
From the selected Route object, the UrlRoutingModule object obtains the IRouteHandler object that is associated with the Route object. Typically, in an MVC application, this will be an instance of MvcRouteHandler. The IRouteHandler instance creates an IHttpHandler object and passes it the IHttpContext object. By default, the IHttpHandler instance for MVC is the MvcHandler object. The MvcHandler object then selects the controller that will ultimately handle the request.
从Route选择的对象后UrlRoutingModule模块获得与之相关的IRoteHandler对象,通常在一个MVC应用程序中,这将是MvcRouteHandler的实例。IRouteHandler实例创建一个IHttpHandler对象并传递到IHttpContext对象中。默认情况下,这个MVC的IHttpHandler实例是MvcHandler对象,最后在MvcHandler对象中选定哪个controller来继续请求。
When an ASP.NET MVC Web application runs in IIS 7.0, no file name extension is required for MVC projects. However, in IIS 6.0, the handler requires that you map the .mvc file name extension to the ASP.NET ISAPI DLL. |
提示: |
如果ASP.NET MVC Web应用程序运行在IIS7.0中允许MVC工程没有文件扩展名的请求,可是IIS 6.0要求你必须为请求处理程序映射护展名为.mvc的ASP.NET ISAPI DLL |
The module and handler are the entry points to the ASP.NET MVC framework. They perform the following actions:
Select the appropriate controller in an MVC Web application.
Obtain a specific controller instance.
Call the controller's Execute method.
ASP.NET MVC framework以handler和module作为切入点,进行了以下的工作:
在MVC Web application中选择相应的contoller。
获取这个controller实例。
调用controller的Execute方法。
The following table lists the stages of execution for an MVC Web project.
|
以下表格是ASP.NET MVC执行周期中所处的阶段:
阶段 | 介绍 |
收到第一次请求 | 在Global.asax文件中将Route对象依次增加到RouteTable对象。 |
执行路由 | UrlRoutingModule模块先从RouteTable集合中依次匹配Route,如果匹配到后就创建RouteData对象,然后使用它来创建RequestContext(IHttpContext)对象。 |
创建请求MVC处理模块 | MvcRouteHandler对象创建一个MvcHandler的实例类型并传入RequestContext的实例 |
创建controller | MvcHandler对象使用RequestContext实例以确定IControllerFactory对象(通常是DefaultControllerFactory类的实例)来创建controller实例。 |
执行controller | The MvcHandler instance calls the controller's Execute method. MvcHandler实例调用controller的Execute方法。 |
调用action | 大多数控制器继承自Controller基类,ControllerActionInvoker对象确实与其关联的Controller类中的action然后调用这个方法。 |
执行结果 | 通常action方法可以接收用户输入并得到结果,然后返回一个result类型。你可以使用几种内置的返回结果: ViewResult(呈现一个视图,这是最常用的返回结果) RedirectToRouteResult、RedirectResult、ContentResult、JsonResult和EmptyResult |