App_Start Folder in ASP 4.5 only in WebApplications Projects?

后端 未结 5 1950
萌比男神i
萌比男神i 2020-12-25 11:44

I have a Website Project I\'ve converted to .NET 4.5. I\'d like to use the AuthConfig that I\'ve seen added to the App_Start

5条回答
  •  不思量自难忘°
    2020-12-25 12:15

    Although There is nothing special about App_Start, but you can make files insinde this folder executes when application started like Application_Start inside Global.asax. I am using Ninject dependency injector in my project which has App_Start folder. There is no Global.asax file in my project:

    enter image description here

    but all configuration i have set in NinjectWebCommon file will be executed upon starting application. NinjectWebCommon has following content:

    using WebFormNinject.Models;
    
    [assembly: WebActivator.PreApplicationStartMethod(typeof(WebFormNinject.App_Start.NinjectWebCommon), "Start")]
    [assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(WebFormNinject.App_Start.NinjectWebCommon), "Stop")]
    
    namespace WebFormNinject.App_Start
    {
        using System;
        using System.Web;
    
        using Microsoft.Web.Infrastructure.DynamicModuleHelper;
    
        using Ninject;
        using Ninject.Web.Common;
    
        public static class NinjectWebCommon 
        {
            private static readonly Bootstrapper bootstrapper = new Bootstrapper();
    
            /// 
            /// Starts the application
            /// 
            public static void Start() 
            {
                DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
                DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
                bootstrapper.Initialize(CreateKernel);
            }
    
            /// 
            /// Stops the application.
            /// 
            public static void Stop()
            {
                bootstrapper.ShutDown();
            }
    
            /// 
            /// Creates the kernel that will manage your application.
            /// 
            /// The created kernel.
            private static IKernel CreateKernel()
            {
                var kernel = new StandardKernel();
                kernel.Bind>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind().To();
    
                RegisterServices(kernel);
                return kernel;
            }
    
            /// 
            /// Load your modules or register your services here!
            /// 
            /// The kernel.
            private static void RegisterServices(IKernel kernel)
            {
                kernel.Bind().To();
            }        
        }
    }
    

    I was curious where RegisterServices function will be executed! Then I noticed to this section of code:

    [assembly: WebActivator.PreApplicationStartMethod(typeof(WebFormNinject.App_Start.NinjectWebCommon), "Start")]
    [assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(WebFormNinject.App_Start.NinjectWebCommon), "Stop")]
    

    These attributes make Start method executed on application started. For more information look at WebActivator / PreApplicationStartMethod

提交回复
热议问题