execute code when starting an ASP.NET MVC 4 Application

前端 未结 3 1566
旧时难觅i
旧时难觅i 2020-12-06 16:19

I want when my application starts, to execute some code

   if (!WebMatrix.WebData.WebSecurity.Initialized){
           WebMatrix.WebData.WebSecurity.Initiali         


        
相关标签:
3条回答
  • 2020-12-06 16:32

    This kind of startup code typically goes in the Application_Start() method, Global.asax.cs file

    0 讨论(0)
  • 2020-12-06 16:43

    Use the following in the Global.asax:

    protected void Application_Start(object sender, EventArgs e)
    {
    
    0 讨论(0)
  • 2020-12-06 16:51

    Put your code in static method inside a class.

    public static class SomeStartupClass
    {
        public static void Init()
        {
            // whatever code you need
        }
    }
    

    Save that in App_Start. Now add it to Global.asax, along with the other code MVC initialises here:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
    
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();
    
        SomeStartupClass.Init();
    }
    

    Now your startup code is separated nicely.

    0 讨论(0)
提交回复
热议问题