在 Application_Start 中设置和运行代码的首选方法

发布于 2024-10-03 00:55:40 字数 145 浏览 4 评论 0原文

在 Application_Start 和 Application_Endrequest 中连接并运行某些代码而不必每次都将代码放入 MvcApplication 类中的首选方法是什么?

也许有一个事件、一个属性或者我可以在 asp.net 4 中连接的东西?

What is the preferred way to hook up and run some code in Application_Start and Application_Endrequest without having to put the code in the MvcApplication class each time?

Maybe there is an event, an attribute or something I can hook up in asp.net 4?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野生奥特曼 2024-10-10 00:55:40

HttpApplication 上有很多 事件类。但是,您仍然需要一些可以引导事件连接的东西。

您可以查看 PreApplicationStartAttribute。它允许您在应用程序编译之前连接要运行的代码(尽管应用程序实例不会那么早可用)。

David Ebbo 还创建了一个类似的系统,名为 WebActivator

There are lots of events on the HttpApplication class. However, you still need something that will bootstrap the event hookup.

You could take a look at PreApplicationStartAttribute. It lets you hook up code to run before the application gets compiled (though the Application instance will not be available that early).

David Ebbo also created a similar system called WebActivator.

从来不烧饼 2024-10-10 00:55:40

这不是一个正确的说法,“让您在应用程序编译之前挂钩代码以运行。

您创建的 WebActivator 类实际上会编译到程序集中,但允许您挂钩 3 个事件运行时:

1.) PreApplicationStartMethod
2.)PostApplicationSTART方法
3.) ApplicationShutdownMethod

来自 David Ebbo 的描述:

“WebActivator 是一个 NuGet 包,它允许其他包轻松地将启动和关闭代码引入到 Web 应用程序中。这提供了比拥有更简洁的解决方案 使用许多包的启动逻辑修改 global.asax。”

使用 WebActivator 不仅限于其他包的启动代码,它还可以用于您的应用程序的启动代码(引导)。

例如,我在最近使用 MVC3 完成的项目中使用它来设置和配置我的 Unity 依赖项、Combres 设置和 51DegreesMobi 设置。它允许我为每个类都有一个单独的类文件,而不会弄乱或在 Global.asax 中输入一行代码。

它也是 .NET 4 PreApplicationStartMethod 的一个更好的替代方案,因为您可以拥有许多具有 WebActivator.PreStartupMethod 的类,并且它们都将在 App-Start 之前运行,而不是我相信您的 .NET 4 属性只允许使用一次。

That is not a correct statement that "lets you hook up code to run before the application gets compiled."

WebActivator classes that you create actually get compiled into the assembly, but allow you to hook into 3 events at run time:

1.) PreApplicationStartMethod
2.) PostApplicationSTartMethod
3.) ApplicationShutdownMethod

From David Ebbo's desciption:

"WebActivator is a NuGet package that allows other packages to easily bring in Startup and Shutdown code into a web application. This gives a much cleaner solution than having to modify global.asax with the startup logic from many packages."

Using WebActivator is not limited to startup code for just other packages, it can also be used for startup code (bootstrapping) for your application as well.

For instance, I use it to set up and configure my Unity dependencies, my Combres setup, and my 51DegreesMobi setup in a recent project I completed using MVC3. It allows me to have a separate class file for each of those without cluttering up or entering a single line of code in Global.asax.

It is also a much better alternative to the .NET 4 PreApplicationStartMethod in that you can have many classes that have the WebActivator.PreStartupMethod and they will ALL run prior to App-Start, as opposed to the .NET 4 attribute in which I believe you're only allowed to use once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文