【问题标题】:How is ASP.NET MVC wired into ASP.NET?ASP.NET MVC 如何连接到 ASP.NET?
【发布时间】:2011-10-23 16:15:32
【问题描述】:

我试图更好地理解 ASP.NET 和 ASP.NET MVC 背后的“plumbing”。我一直在阅读this page,这对我很有帮助。据我所知,到目前为止,每个 ASP.NET 站点都有一个继承自 System.Web.HttpApplication 的类。 HttpApplication 然后有一系列触发 HttpModules 和 HttpHandlers 的事件,例如 BeginRequest、AuthorizeRequest、End Request 等......然后 HttpModules 和 HttpHandlers 读取和写入当前 HttpContext。

ASP.NET 如何知道要使用哪个 HttpApplication 类?我的应用程序在 Global.asax 文件中有典型的 MvcApplication 类。但是我在这个类中看不到任何与 MVC 相关的东西。我也看不到任何将此类分配为“应用程序”的设置。 ASP.NET 是否总是查找名为 Global.asax 的文件来确定要创建的 HttpApplication 类?还是 ASP.NET 只是在我的程序集中查找从 HttpApplication 继承的任何类?

另外,它如何知道要使用哪些模块和处理程序?我上面提到的页面说您通过 web.config 中的设置来指定处理程序和模块。但是我的 ASP.NET MVC 应用程序的 web.config 中没有这些设置?

如果我在其中一个操作方法中设置断点并检查 HttpContext.Current.ApplicationInstance.Modules 我会看到以下内容:

OutputCache
Session
WindowsAuthentication
FormsAuthentication
PassportAuthentication
RoleManager
UrlAuthorization
FileAuthorization
AnonymousIdentification
Profile
ErrorHandlerModule
ServiceModel
UrlRoutingModule-4.0
ScriptModule-4.0
__DynamicModule_System.Web.WebPages.WebPageHttpModuleDefaultAuthentication

这些是在哪里指定的?同样,如果我检查 HttpContext.Current.Handler,我可以看到它设置为 System.Web.Mvc.MvcHandler

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    好吧,您可以阅读源代码。可在 codeplex.com 上获得

    您还可以观看 Scott Hanselman 的视频,第一部分稍微深入一点(虽然不是太深入)。

    http://channel9.msdn.com/Blogs/matthijs/ASPNET-MVC-2-Ninja-Black-Belt-Tips-by-Scott-Hanselman

    【讨论】:

      【解决方案2】:

      当第一个用户访问您的网站时:

      1) 它加载所有 web.config 指定的所有 Http 模块,这些模块与您的应用程序相关。

      2) 如果系统支持 ASP.NET MVC,则global web.config 注册UrlRoutingModule,将其合并到请求管道中。

      现在,您从 global.asax 中的 HttpApplication 派生的类(例如标准 MvcApplication)被编译到您的 dll 中。牢记这一点...

      5) ASP.NET 运行时扫描 YourApplication.dll 以查找派生自 HttpApplication 的类并执行其许多方法(例如,Application_Start)。

      6) 当您使用惯用的 ASP.NET MVC MapRoute 扩展方法创建路由时,它会将路由与 MvcRouteHandler 相关联。

      7) UrlRoutingModule(来自第 2 步)使用 routing handler 选择 http 处理程序 ASP.NET 将用于处理该路由上的传入请求。

      你可以找到the full story here

      【讨论】:

      • 完整的故事链接总结得很好。添加到问题的未回答部分,ASP.NET 运行时从根 Global.asax 文件创建 HttpApplication 实例。
      • 感谢 marcind - 我完全忽略了问题的那一部分。
      猜你喜欢
      • 2018-01-07
      • 2017-01-31
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多