2013-01-09 25 views
24

Tôi có một Dự án trang web tôi đã chuyển đổi thành .NET 4.5. Tôi muốn sử dụng AuthConfig mà tôi đã thấy được thêm vào thư mục App_Start. Một vài câu hỏi.Thư mục App_Start trong ASP 4.5 chỉ trong các Dự án Ứng dụng Web?

Thư mục App_Start chỉ khả dụng cho các dự án ứng dụng web ?, Khi tôi cố thêm thư mục asp.net hiện tại, tôi không thấy thư mục này là tùy chọn được thêm vào.

Thứ hai, tôi có thể chỉ là một tệp AuthConfig ở bất kỳ đâu trong dự án trang web của tôi nếu trường hợp đó xảy ra không?

Trả lời

2

Tóm tắt: Để hiểu rõ hơn về thay đổi cấu hình trong trang web ASP.NET 4.5, hãy xem nguồn chính thức sau - Configuration Changes in ASP.NET 4.5 Website Templates.

Nó sẽ hướng dẫn bạn trên mỗi sự thay đổi đó đã xảy ra trên phiên bản mới hơn trên ASP.NET trên trang web, là 4,5

18

Không có gì đặc biệt về App_Start là, nó chỉ là một thư mục. Điều đặc biệt là cách nó được sử dụng, và đó là đặc trưng cho khung công tác WebActivator, đây là một gói NuGet mà bạn có thể cài đặt. App_Start và WebActivator không đặc hiệu với .NET 4.5, nhưng chúng đòi hỏi .net 4 (có nghĩa là VS 2010 hoặc 2012)

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

17

Thư mục App_Start đã được giới thiệu như là một phần của các mẫu MVC4. Không có gì đặc biệt về nó khiến cho mã được thực hiện trong nó theo quy ước. Ví dụ, mẫu HotTowel SPA tạo ra sau trong thư mục App_Start:

TODO diagram

Mã trong App_Start được thực hiện bởi global.asax.cs như hình dưới đây.

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

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

Tất cả các lớp đó được tham chiếu trong Global.asax ở đâu? Một số tham chiếu phải ở đâu đó, chúng không được tham chiếu trực tiếp. – FrenkyB

11

Mặc dù There is nothing special about App_Start, nhưng bạn có thể làm cho các file insinde thư mục này thực hiện khi ứng dụng bắt đầu như Application_Start bên Global.asax. Tôi đang sử dụng Ninject injency injector trong dự án của tôi trong đó có App_Start thư mục. Có không có tập tin Global.asax trong dự án của tôi:

enter image description here

nhưng tất cả cấu hình tôi đã thiết lập trong NinjectWebCommon tập tin sẽ được thực hiện khi bắt đầu ứng dụng. NinjectWebCommon có nội dung sau:

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

Tôi đã tò mò nơi chức năng RegisterServices sẽ được thực thi! Sau đó, tôi nhận thấy phần này của mã:

[assembly: WebActivator.PreApplicationStartMethod(typeof(WebFormNinject.App_Start.NinjectWebCommon), "Start")] 
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(WebFormNinject.App_Start.NinjectWebCommon), "Stop")] 

Các thuộc tính này làm cho phương thức Start được thực hiện khi bắt đầu ứng dụng. Để biết thêm thông tin, hãy xem WebActivator/PreApplicationStartMethod

+0

Liên kết tuyệt vời bạn tìm thấy về giải thích WebActivator!+1 cho bạn! – FrankO

+0

Giải thích tuyệt vời. Phương thức "Start" và "Stop" của một tệp trong thư mục App_Start được gọi tự động khi các thuộc tính assembly được chỉ định. Những lưu một số dòng mã trong tập tin global.asax.cs. –

Các vấn đề liên quan