我有一个类,其方法应在应用程序启动时运行。我不想直接从 Application_Start 事件调用此方法。实例化此类并在 application_start 上运行方法的最佳方法是什么?
换句话说,我想将这段代码注入(inject)到应用程序启动中。
最佳答案
我注意到有些人使用 WebActivatorEx.PostApplicationStartMethod。我没有深入研究细节,但这是我首先要看的地方。下面是注册为在调用 RegisterBundles 时自动运行的类的示例。其他 Hook 之一可能就是您正在寻找的东西。
[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"));
}
}
}
关于c# - MVC : Run a method on Application startup without calling it from Application_Start,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23311217/