如果我尝试同时使用Start和OnStart,那么OnStart不会被调用(而且设置失败,导致错误)。看起来我必须选择一个。
我的问题是:我应该使用哪一个?它们之间有什么区别?它们在不同的时间被调用吗?
(注意:在撰写本文时,“top three Google”的搜索结果无用或误导性。我希望Stack Overflow可以解决这个问题。)
这些基本上是硬编码到经典ASP运行时中的 - 您无法更改它们,并且无法将任何其他方法附加到这些事件。
在ASP.NET中,有一个称为AutoEventWireup
的东西,它使用反射查找符合特定命名约定的方法,并在响应由ASP.NET运行时引发的匹配事件时运行这些方法。最常见的例子是Page_Load
方法,它会自动调用以响应页面生命周期中Page类触发的Load事件。
相同的技术用于附加处理程序到应用程序级别的生命周期事件。它会查找命名为ModuleName_EventName或ModuleName_OnEventName的方法,不带参数()
或(object sender, EventArgs e)
这里是有趣的部分-如果您定义了多个匹配方法,则只执行出现在文件中最新的方法。 (基本上是最后一个方法获胜)
因此,如果您的global.asax.cs看起来像这样:
public class Global : System.Web.HttpApplication {
protected void Application_Start() {
Debug.WriteLine("A: Application_Start()");
}
protected void Application_Start(object sender, EventArgs e) {
Debug.WriteLine("B: Application_Start(object sender, EventArgs e)");
}
protected void Application_OnStart() {
Debug.WriteLine("C: Application_OnStart()");
}
protected void Application_OnStart(object sender, EventArgs e) {
Debug.WriteLine("D: Application_OnStart(object sender, EventArgs e)");
}
}
如果你注释掉该块中的最后一个方法,你将在调试输出中看到消息C;如果不注释,则看到消息D。
所以,使用任何你喜欢的命名约定都可以,但是如果你定义多个,只有在源文件中最后出现的一个会被执行。我个人会坚持使用Application_Start(object sender, EventArgs e)
,因为这是由Visual Studio项目模板和大多数.NET设计/编码工具生成的签名。
Application_Start
方法:
W3 学校介绍了 ASP (旧版本?)中更多关于
Application_Start
:当请求 ASP.NET 应用程序中的第一个资源(如页面)时调用。应用程序生命周期中只调用一次 Application_Start 方法。你可以使用此方法执行启动任务,例如将数据加载到缓存中并初始化静态值。
Application_OnStart
的内容。这个方法放在 Global.asa 文件中,而不是 Global.asax。
参见此答案的评论,其中建议
Application_OnStart
:在创建第一个新会话之前(当首次引用 Application 对象时)触发 Application_OnStart 事件。将此事件放置在 Global.asa 文件中。
Application_OnStart
适用于旧版 ASP,而 Application_Start
适用于 ASP.NET。
Application_OnStart
函数在处理任何 .asp 文件之前被调用,也就是在任何文本或图形被呈现并发送到用户浏览器之前。在此函数中,对 Active Server Pages Server 对象上的 CreateObject 方法进行以下调用,创建 CDO Rendering Library RenderingApplication 对象。如果此调用成功,则 objRenderApp 变量包含指向新对象的指针。
我找到的所有参考资料都是关于 .asp 页面的。
Application_Start
和Application_End
方法是特殊方法,它们不代表 HttpApplication 事件。ASP.NET 为应用程序域的生命周期调用它们一次,而不是为每个 HttpApplication 实例调用。
此页面涉及 .aspx 页面。因此,您正在使用 MVC 并提到 global.asax,这就是您应该使用的方法。