App_Start Folder in ASP 4.5 only in WebApplications Projects?
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();
}
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
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