仅在WebApplications项目中的ASP 4.5中的App_Start文件夹?

Tre*_*reK 28 asp.net app-startup .net-4.5

我有一个网站项目我已经转换为.NET 4.5.我想用AuthConfig,我已经看到添加到App_Start目录.几个问题.

App_Start目录仅适用于Web应用程序项目?当我尝试添加现有的asp.net文件夹,我不认为这是要添加的选项.

其次,如果是这样的话,我可以在我的网站项目中的任何地方使用AuthConfig文件吗?

Eri*_*sch 19

App_Start没有什么特别之处,它只是一个文件夹.它的特殊之处在于它是如何使用的,而且特定于WebActivator框架,它是一个可以安装的NuGet包.App_Start和WebActivator不是特定于.NET 4.5,但它们确实需要.net 4(这意味着VS 2010或2012)

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


Nic*_*yan 19

App_Start文件夹是作为MVC4模板的一部分引入的.它没有什么特别之处,导致代码按惯例执行.例如,HotTowel SPA模板在App_Start文件夹中创建以下内容:

TODO图

App_Start中的代码由global.asax.cs执行,如下所示.

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

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();
    }
Run Code Online (Sandbox Code Playgroud)

  • Global.asax中引用的所有类都在哪里?某些引用必须在某处,它们不是直接引用的. (2认同)

Sey*_*avi 12

虽然没有什么特别之处App_Start是的,但你可以让文件insinde该文件夹时,执行应用程序启动喜欢Application_Start里面Global.asax.我在我的项目中使用Ninject依赖注入器,它有App_Start文件夹.有没有Global.asax中在我的项目文件:

在此输入图像描述

但是我在NinjectWebCommon文件中设置的所有配置都将在启动应用程序时执行.NinjectWebCommon有以下内容:

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>();
        }        
    }
}
Run Code Online (Sandbox Code Playgroud)

我很好奇RegisterServices函数将被执行的地方!然后我注意到这部分代码:

[assembly: WebActivator.PreApplicationStartMethod(typeof(WebFormNinject.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(WebFormNinject.App_Start.NinjectWebCommon), "Stop")]
Run Code Online (Sandbox Code Playgroud)

这些属性使Start应用程序启动时执行方法.有关更多信息,请查看WebActivator/PreApplicationStartMethod