如何在 asp.net-mvc 中连接 Application_BeginRequest()
我在 ASP.NET MVC 项目的 global.asax.cs
中看到,
protected void Application_BeginRequest()
{
}
但是当我尝试将其添加到我的项目中时,我看不到是什么在调用此方法。我看到基本的 System.Web.HttpApplication 有此事件,但我没有看到任何覆盖它或订阅此事件的内容。
有人可以解释一下如何在 ASP.NET MVC 中连接 Application_BeginRequest
吗?
I see in global.asax.cs
from an ASP.NET MVC project
protected void Application_BeginRequest()
{
}
but when I try to add this to my project, I don't see what is calling this method. I see that the base System.Web.HttpApplication
has this event but I don't see anything overriding it or subscribing to this event.
Can someone explain how you wire up Application_BeginRequest
in ASP.NET MVC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恐怕Cos的回答不太准确。您不必连接它,因为
HttpApplication
基类会为您完成此操作。这里没有接口或覆盖;HttpApplication
使用反射根据方法名称将这些方法连接到事件。所以这是一个基于约定的魔法,已经在框架中存在了一段时间。这很奇怪,我认为他们这样做只是为了保持与当时经典 ASP 的相似性和/或提供一种快捷方式以避免编写大量小的HttpModule
。如果好奇的话,可以在 Reflector 中加载 HttpApplication 并寻找 HookupEventHandlersForApplicationAndModules() 方法。或者,加载
HttpApplicationFactory
并查看ReflectOnApplicationType()
和ReflectOnMethodInfoIfItLooksLikeEventHandler()
(喜欢这个名字!)方法。Rick Strahl 在几年前他的博客。
使用 ASP.NET 真的需要知道这些吗?不,但了解它肯定会消除一些神秘感。
I'm afraid Cos's answer isn't quite accurate. You don't have to wire it up because the base
HttpApplication
class does it for you. There isn't an interface or an override here;HttpApplication
uses reflection to hook up these methods to the events based on the method name. So it's a bit of convention-based magic that has been in the framework for a while. It's very strange, and I think they only did it to maintain similarities with Classic ASP back in the day and/or provide a shortcut to avoid writing lots of smallHttpModule
s.For the curious, load up
HttpApplication
in Reflector and hunt for theHookupEventHandlersForApplicationAndModules()
method. Or, loadHttpApplicationFactory
and look at theReflectOnApplicationType()
andReflectOnMethodInfoIfItLooksLikeEventHandler()
(love that name!) methods.Rick Strahl discussed this on his blog a few years ago.
Is it something you really need to know to use ASP.NET? No, but knowing it certainly removes some of the mystery.
任何 ASP.NET 应用程序都是一个对象(或类),其类型为:
public class Global:System.Web.HttpApplication
(您将在 global.asax 中找到此内容)
IIS 调用的 ASP.NET 引擎会创建对象的实例,并且 HttpApplication 接口需要 Application_BeginRequest,IIS 会调用该实例(通过 ISAPI )
当 ASP.NET 引擎创建类的实例时,它看起来像这样:
因为它将您的应用程序转换为派生类型,所以可以直接访问已知接口,而无需覆盖。虽然 HttpApplication 是一个类,但它通过强制转换用作接口。如果您向类添加新方法(或属性),ASP.NET 引擎将无法访问该方法,因为它只将您的应用程序识别为通用 HttpApplication。在 VS 中,如果您转到 global.asax 并右键单击类声明中的 HttpApplication,然后选择“转到定义”(或按 F12),您可以看到基类的结构。 (或者您可以在 MSDN 在线找到它)。
Any ASP.NET application is an object (or class) of type :
public class Global : System.Web.HttpApplication
(you will find this in the global.asax)
The ASP.NET engine invoke by IIS creates an instance of your object and the HttpApplication interface demands Application_BeginRequest, which is invoke by IIS (by way of the ISAPI)
When the ASP.NET Engine creates an instance of your class it looks like this:
Because it casts your app as a derived type, the known interface can be directly accessed without need for overrides. While HttpApplication is a class it is being used as an interface by way of casting. If you add a new method (or property) to your class the ASP.NET engine can not access that method because it is only aware of your application as a generic HttpApplication. In VS if you go to the global.asax and right click over HttpApplication in the class declaration and select "Go To Definition" (or press F12) you can see the structure of base class. (or you can find it in MSDN online).