I am trying to learn MVC in detail, and I am wondering what's the exact functional flow internally, in the sense of which functions (important functions) are called and what they do when the application starts and what functions are called apart from the controller actions that we write in our application as we proceed.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Here are the detailed steps:
- Request comes into ASP.NET
- ASP.NET Routing finds the route match by calling RouteCollection.GetRouteData
- This in turn calls RouteBase.GetRouteData on each route until it finds a match
- The IRouteHandler for the matching route has its GetHttpHandler method called
- The MvcHandler runs (ProcessRequest is called)
- The MVC controller factory locates and creates the controller in CreateController
- The ControllerActionInvoker determines which action to run in InvokeAction
- The AuthorizationFilter stage executes (this includes the authorization method on the controller itself)
- The ActionExecuting stage executes
- The requested action method is executed
- The ActionExecuted stage executes
- If there is a result object then the ResultExecuting stage executes
- If the result wasn't cancelled then the ActionResult's ExecuteResult method is executed
- The ResultExecuted stage executes
- If an error occured then the Exception stage executes
I would also like to refer you to the MVC Snake Diagram that I use in many presentations on ASP.NET MVC. Here's the full image: alt text http://weblogs.asp.net/blogs/leftslipper/WindowsLiveWriter/ASP.NETMVCDesignPhilosophy_F491/MVC_Snake_2.png
The blog post I linked to describes some of the concepts used in ASP.NET MVC regarding how data flows through the application.
回答2:
Check out Redgates's free "The ASP.NET MVC Request Handling Pipeline" poster for execution flow
and Steven Sanderson's MCV book for details.