App_Start Folder in ASP 4.5 only in WebApplications Projects?

不问归期 提交于 2019-11-30 01:30:13

There is nothing special about App_Start, it's just a folder. What's special is how it's used, and that's specific to the WebActivator framework, which is a NuGet package you can install. App_Start and WebActivator are not specific to .NET 4.5, but they do require .net 4 (which means VS 2010 or 2012)

See http://blog.davidebbo.com/2011/02/appstart-folder-convention-for-nuget.html

The App_Start folder was introduced as part of the MVC4 templates. There is nothing special about it that causes code to be executed in it by convention. For example, the HotTowel SPA template creates the following in the App_Start folder:

Code in the App_Start is executed by global.asax.cs as shown below.

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();
    }
Seyed Morteza Mousavi

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:

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();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            RegisterServices(kernel);
            return kernel;
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IDisplay>().To<MockDisplay>();
        }        
    }
}

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

In short: For a deeper understanding the configuration changes in ASP.NET 4.5 Websites, look at the following official source - Configuration Changes in ASP.NET 4.5 Website Templates.

It will instruct you on each change that happened on newer version on ASP.NET Website, which is 4.5

If you want it for configuring your routing map on the new MVC 5 templates you can set your routes on the Startup.cs file in the Configure method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!