【问题标题】:MVC: Run a method on Application startup without calling it from Application_StartMVC:在应用程序启动时运行一个方法,而不从 Application_Start 调用它
【发布时间】:2014-04-26 21:27:40
【问题描述】:

我有一个类,它的方法应该在应用程序启动时运行。我不想直接从 Application_Start 事件中调用此方法。使此类实例化并在 application_start 上运行方法的最佳方法是什么?

换句话说,我想将这段代码注入到应用程序启动中。

【问题讨论】:

  • 请问您为什么不想在Application_Start中调用需要在应用启动时运行的代码?
  • 我有一个 CMS 应用程序和多个使用此 CMS 的网站(mvc 区域)。我不想更改实际 CMS 的 application_start,因为我可以在不更改区域的情况下运行 CMS 更新。所以这些区域应该有一些在启动时注册自己的代码

标签: c# asp.net-mvc


【解决方案1】:

我注意到有些人使用 WebActivatorEx.PostApplicationStartMethod。我没有深入研究细节,但这是我首先要看的地方。这是一个注册为在调用 RegisterBundles 时自动运行的类的示例。其他钩子之一可能是您正在寻找的。​​p>

[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"));
        }
    }
}

【讨论】:

    【解决方案2】:

    使用 OWIN 启动的可能解决方案之一。

    安装nuget包:install-package Microsoft.Owin.Host.SystemWeb

    添加到appsettings启动类:

       <appSettings>
              <add key="owin:appStartup" value="MyProject.Code.Startup" />
       </appSettings>
    

    按照惯例,您将需要带有名为 Configuration 的方法的类:

        public class Startup
        {
                public void Configuration(IAppBuilder app)
                {
                    app.Run(context =>
                    {
                        string t = DateTime.Now.Millisecond.ToString();
                        return context.Response.WriteAsync(t + " Production OWIN App");
                    });
                }
        }
    

    或者做任何你需要的事情。

    如果您对此感兴趣,请查看 asp.net:OWIN and Katana project

    【讨论】:

    • +1。谢谢你,但另一个答案对我们的网络应用程序来说有点容易。
    【解决方案3】:

    使用委托。它们持有对方法的引用,这使您可以一次调用某些方法; 使用委托的示例:

    public delegate void myDelegate();
    
    private static void Main()
    {
        myDelegate myFunctions = Method1; //initialize delegate with Method1
        myFunctions += Method2;           //Add Method2 to delegate
    
        myFunctions(); //call all methods added to delegate
    }
    
    public static void Method1()
    {
        Console.WriteLine("Hello from method1");
    }
    
    public static void Method2( )
    {
        Console.WriteLine("Hello from method2");
    }
    

    这将同时调用 Method1 和 Method2

    【讨论】:

    猜你喜欢
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    相关资源
    最近更新 更多