c# - MVC : Run a method on Application startup without calling it from Application_Start

标签 c# asp.net-mvc

我有一个类,其方法应在应用程序启动时运行。我不想直接从 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/

相关文章:

asp.net-mvc - ASP.NET MVC 2 中的 Web.config 转换和额外的 Web.config 文件

c# - 使用 C# 调用带参数的 SQL Server 存储过程

c# - 将鼠标直接放在 Silverlight 中的元素上

c# - Web.UI 的 InvokeRequired 需要帮助

css - UI-SREF 在 _layout.cshtml 中不起作用

asp.net - MVC beta 中的 <%=Html.RadioButtonList()%> 扩展在哪里?

asp.net-mvc - 避免 ASP.NET MVC 中的意大利面条式代码

c# - Visual Studio 自动格式化没有正确格式化我的 Razor 代码

c# - 如何使用 Selenium 检查复选框状态?

c# - 如何从手机和SD卡中获取音频文件并进行播放?