gpt4 book ai didi

asp.net - 何时在 Global.asax 中使用 Application_Start 与 Init?

转载 作者:行者123 更新时间:2023-12-03 05:18:32 25 4
gpt4 key购买 nike

我想知道在什么情况下我应该将应用程序初始化代码放在 Application_Start() 中,而不是放在 Global.asax 中的 Init() 中文件?

对我来说,两者之间的区别似乎不是很明显,除了先调用 Application_start,然后调用 Init()

  • 为什么我要使用其中一种而不是另一种?
  • 这真的有区别吗?
  • 应用程序状态发生了什么变化两个事件之间?

到目前为止,我能找到的唯一真正的指针是 IHttpModule 只有一个 Init() 方法,所以如果我正在做的事情在某些时候可能会更好适合实现 IHttpModule 我应该使用 Global.asaxInit() 方法,如果没有其他一致性的话。

最佳答案

来自MSDN docs :

The Application_Start and Application_End methods are special methods that do not represent HttpApplication events. ASP.NET calls them once for the lifetime of the application domain, not for each HttpApplication instance.

初始化:

Called once for every instance of the HttpApplication class after all modules have been created.

更新:如果您需要确保某个代码仅在应用程序中调用一次。生命周期,Application_Start是一个更好的解决方案。示例:配置 log4net?

关于asp.net - 何时在 Global.asax 中使用 Application_Start 与 Init?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/720969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com