App_Start Folder in ASP 4.5 only in WebApplications Projects?
Asked Answered
G

5

31

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?

Giacopo answered 9/1, 2013 at 3:58 Comment(0)
G
22

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

Gillispie answered 9/1, 2013 at 4:51 Comment(0)
A
22

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:

TODO diagram

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();
    }
Antirachitic answered 26/9, 2013 at 21:22 Comment(1)
Where are all those classes referenced in Global.asax? Some reference must be somewhere, they are not directly referenced.Seine
L
13

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

        /// <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

Lcm answered 7/2, 2014 at 10:40 Comment(2)
Great link you found about the WebActivator explanation! +1 for you!Gynous
Great explanation. Methods of "Start" and "Stop" of a file in App_Start folder are called automatically when assembly attributes are specified. Those save some code lines in global.asax.cs file.Gadolinite
U
2

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

Undulant answered 9/1, 2013 at 4:11 Comment(0)
T
1

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

Thrombin answered 17/10, 2018 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.