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 directory. A couple of questions.
Is the App_Start directory only available for Web Application Projects?, When I try to add an existing asp.net folder I don't see it as an option to be added.
Secondly, Can I just an AuthConfig file anywhere in my website project if that is the case?
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();
}
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