的Application_Start Application_OnStart的和区别 [英] Difference between Application_Start and Application_OnStart

查看:358
本文介绍了的Application_Start Application_OnStart的和区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在加入ASP.NET MVC code到A preexisting ASP.NET Web表单项目的过程。 href=\"http://www.packtpub.com/article/mixing-asp.net-webforms-and-asp.net-mvc\"> 建议增加路由的方法,各种教程从的Application_Start称为

I'm in the process of adding ASP.NET MVC code to a preexisting ASP.NET Webforms project. The various tutorials suggest adding routing to a method called from Application_Start() in Global.asax. My Global.asax already has an Application_OnStart(Object,EventArgs) method with some setup code.

如果我试图同时拥有启动和的OnStart的的OnStart不会被调用(和安装失败,从而导致错误)。它看起来像我必须选择一个或其他。

If I try to have both Start and OnStart, the OnStart doesn't get called (and the setup fails, causing errors). It looks like I have to choose one or the other.

我的问题是:我应该使用哪一个?它们之间有什么区别?他们是所谓的在不同的时间?

My question is: which one should I be using? What is the difference between them? Are they called at different times?

(注意:在写这篇文章的时候,顶部three <一个href=\"http://bytes.com/topic/asp-net/answers/325099-http-module-attaching-application_onstart\">Google点击率是无用的和/或误导性的。我希望堆栈溢出​​可以解决这个问题。)

(Note: at the time of this writing, the top three Google hits are useless and/or misleading. I'm hoping Stack Overflow can fix that.)

推荐答案

在经典(传统)ASP,也有特殊功能的名称屈指可数,如果在你的global.asa文件中定义,将在指定点期间运行应用程序生命周期。这些被定义为:

In classic (legacy) ASP, there are a handful of special function names that, if defined in your global.asa file, will be run at specified points during the application lifecycle. These are defined as:


  • Application_OnStart的 - 运行一次,当你的应用程序接收到的第一个HTTP请求和所有.asp文件处理之前立即

  • Application_OnEnd - 所有的请求都被处理后,运行一次,在应用程序关闭,

  • 的Session_OnStart - 在每一个独特的用户会话的开始运行。如果用户/客户端的cookie禁用,这会运行每一个请求,因为ASP从未检测识别现有会话的会话cookie。

  • 的Session_OnEnd - (!理论上)每个用户会话过期时间运行。祝你好运与此有关。

这些基本上是硬连接到传统的ASP运行 - 你不能改变他们,你可以不是任何其他方式连接到这些事件

These are basically hard-wired into the classic ASP runtime - you can't change them, and you can't attach any other methods to these events.

在ASP.NET中,有一个名为 AutoEventWireup 东西,使用反射来寻找符合特定的命名约定方法,并运行在回应募集匹配事件的那些方法ASP.NET运行时。最常见的例子是的Page_Load 方法,这是在响应Page类的页面生命周期中触发的Load事件自动调用。

In ASP.NET, there's a thing called AutoEventWireup that uses reflection to find methods conforming to particular naming conventions, and runs those methods in response to matching events raised by the ASP.NET runtime. The most common example is the Page_Load method, which is automatically invoked in response to the Page class firing the Load event during the page lifecycle.

同样的技术用于连接处理器到应用层的生命周期事件。它会寻找命名为ModuleName_EventName或ModuleName_OnEventName方法,并带有任何参数()(对象发件人,EventArgs五)

The same technique is used to attach handlers to application-level lifecycle events. It will look for methods named either ModuleName_EventName or ModuleName_OnEventName, taking either no parameters () or (object sender, EventArgs e)

下面是有趣的部分 - 如果您定义多个匹配的方法,只有在文件中出现最晚将执行之一。 (最后一个方法获胜,基本)

Here's the fun part - if you define more than one matching method, only the one that appears latest in the file will execute. (The last method wins, basically)

所以,如果你的global.asax.cs看起来是这样的:

So if your global.asax.cs looks like this:

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)");
    }
}

您会看到消息d在调试输出;如果你在一个块注释掉最后一个方法,你会看到消息C来代替。

you'll see message D in your debug output; if you comment out the last method in that block, you'll see message C instead.

所以 - 使用任何命名你喜欢的约定,但如果你定义多个,仅出现在源文件中最后一个将要执行的之一。我个人坚持使用的Application_Start(对象发件人,EventArgs五)因为这是由Visual Studio项目模板和大多数.NET设计/编码工具生成的签名。

So - use whichever naming convention you like, but if you define more than one, only the one that appears last in your source file will be executed. I would personally stick with Application_Start(object sender, EventArgs e) since that's the signature generated by the Visual Studio project templates and most .NET design/coding tools.

这篇关于的Application_Start Application_OnStart的和区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆