Understanding the ASP.NET MVC Execution Process
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.
UrlRoutingModule选择第一个匹配的route object
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.
UrlRoutingModule会从选择的route object中获取和route object相关的route handler,一般来讲是MvcRouteHandler
Route Handler会创建一个HttpHandler,并且传递HttpContext给HttpHandler,一般来讲是MvcHandler。MvcHandler负责选择Controller
Note
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.
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.
The following lists the stages of execution for an MVC Web project:
-
Receive first request for the application
- In the Global.asax file, Route objects are added to the RouteTable object.
-
Perform routing
- The UrlRoutingModule module uses the first matching Route object in the RouteTable collection to create the RouteData object, which it then uses to create a RequestContext (IHttpContext) object.
-
Create MVC request handler
- The MvcRouteHandler object creates an instance of the MvcHandler class and passes it the RequestContext instance.
-
Create controller
- The MvcHandler object uses the RequestContext instance to identify the IControllerFactory object (typically an instance of the DefaultControllerFactory class) to create the controller instance with.
-
Execute controller - The MvcHandler instance calls the controller s Execute method. |
-
Invoke action
- Most controllers inherit from the Controller base class. For controllers that do so, the ControllerActionInvoker object that is associated with the controller determines which action method of the controller class to call, and then calls that method.
-
Execute result
- A typical action method might receive user input, prepare the appropriate response data, and then execute the result by returning a result type. The built-in result types that can be executed include the following: ViewResult (which renders a view and is the most-often used result type), RedirectToRouteResult, RedirectResult, ContentResult, JsonResult, and EmptyResult.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2018-08-20 Serializable and XmlEnum