I'm trying to gain a better understanding of the "plumbing" behind ASP.NET and ASP.NET MVC. I've been reading this page, which has helped a lot. From what I understand so far every ASP.NET site has a class that inherits from System.Web.HttpApplication. HttpApplication then has a series of events that trigger HttpModules and HttpHandlers such as Be开发者_如何学JAVAginRequest, AuthorizeRequest, End Request etc... The HttpModules and HttpHandlers then read and write to and from the current HttpContext.
How does ASP.NET know what HttpApplication class to use? My application has the typical MvcApplication class in a Global.asax file. But I don't see anything in this class related to MVC. Nor do I see any settings anywhere that assign this class as being the "application". Does ASP.NET just always look for a file named Global.asax to figure out what HttpApplication class to create? Or does ASP.NET just look for any class that inherits from HttpApplication in my assembly?
Also, how does it know what modules and handler to use? The page I mentioned above said you specify the handlers and modules through and settings in web.config. But my ASP.NET MVC application doesn't have these settings in its web.config?
If I set a break point in one of my action methods and check HttpContext.Current.ApplicationInstance.Modules I see the following:
OutputCache
Session
WindowsAuthentication
FormsAuthentication
PassportAuthentication
RoleManager
UrlAuthorization
FileAuthorization
AnonymousIdentification
Profile
ErrorHandlerModule
ServiceModel
UrlRoutingModule-4.0
ScriptModule-4.0
__DynamicModule_System.Web.WebPages.WebPageHttpModuleDefaultAuthentication
Where were these specified? Likewise if I check HttpContext.Current.Handler I can see that it's set to a System.Web.Mvc.MvcHandler
.
When the first user hits your site:
1) It loads all the Http Modules specified by all the web.configs that bear on your application.
2) If the system is ASP.NET MVC-enabled, a global web.config registers a UrlRoutingModule
, incorporating it into the request pipeline.
Now, the class you derive from HttpApplication
in your global.asax (such as the standard MvcApplication
) is compiled into your dll. Bearing that in mind ...
5) The ASP.NET runtime scans YourApplication.dll for a class that derives from HttpApplication
and executes a number of its methods (e.g., Application_Start
).
6) When you create routes using the idiomatic ASP.NET MVC MapRoute
extension method, it associates the route with an MvcRouteHandler
.
7) The UrlRoutingModule
(from step 2) uses that routing handler to select the http handler ASP.NET will use to process the incoming request on that route.
You can find the full story here.
Well, you can read the source. It's available at codeplex.com
You can also watch Scott Hanselman's video, the first part goes into it a little (though not too deeply).
http://channel9.msdn.com/Blogs/matthijs/ASPNET-MVC-2-Ninja-Black-Belt-Tips-by-Scott-Hanselman
精彩评论