MVC:运行在应用程序启动的方法而不调用的Application_Start它 [英] MVC: Run a method on Application startup without calling it from Application_Start
问题描述
我有,应该在应用程序启动时运行的方法的类。我不想直接从Application_Start事件调用此方法。什么是得到的Application_Start这个类实例化和方法运行的最好方法是什么?
I have a class with a method that should run on application startup. I don't want to call this method directly from Application_Start event. What's the best way to get this class instantiated and method run on application_start?
在换句话说,我想注入这些代码到应用程序的启动。
In other words, I want to inject this code into application startup.
推荐答案
我注意到,有些人使用WebActivatorEx.PostApplicationStartMethod。我没有深入探讨细节,但它是我想看看第一的位置。下面是注册时RegisterBundles被称为自动运行的类的实例。其中一个其他的挂钩可能是你在找什么。
I've noticed that some people use WebActivatorEx.PostApplicationStartMethod. I've not delved into the details but it is the first place I would look. Here's an example of a class registered to automatically run when RegisterBundles is called. One of the other hooks may be what you are looking for.
[assembly: WebActivatorEx.PostApplicationStartMethod(typeof(BootstrapBundleConfig), "RegisterBundles")]
namespace Deloitte.EmploymentMemo.Presentation.App_Start
{
public class BootstrapBundleConfig
{
public static void RegisterBundles()
{
// Add @Styles.Render("~/Content/bootstrap") in the <head/> of your _Layout.cshtml view
// For Bootstrap theme add @Styles.Render("~/Content/bootstrap-theme") in the <head/> of your _Layout.cshtml view
// Add @Scripts.Render("~/bundles/bootstrap") after jQuery in your _Layout.cshtml view
// When <compilation debug="true" />, MVC4 will render the full readable version. When set to <compilation debug="false" />, the minified version will be rendered automatically
BundleTable.Bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include("~/Scripts/bootstrap.js"));
BundleTable.Bundles.Add(new StyleBundle("~/Content/bootstrap").Include("~/Content/bootstrap.css"));
BundleTable.Bundles.Add(new StyleBundle("~/Content/bootstrap-theme").Include("~/Content/bootstrap-theme.css"));
}
}
}
这篇关于MVC:运行在应用程序启动的方法而不调用的Application_Start它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!