2011-10-04 94 views
2

ASP.NET MVC 3應用程序的正確安裝我剛添加的Ninject.MVC3 NuGet包(v2.2.2.0)到我的ASP.NET MVC 3應用程序。與Ninject.MVC3 NuGet包

我似乎已經建立了我的ASP.NET MVC 3應用兩種不同的方式:

  1. 通過在Global.asax.cs中
  2. NinjectHttpApplication繼承通過在RegisterServices方法加載NinjectModule NinjectMVC3.cs

現在我試圖理解this blog entry,引用here。這似乎是說還有另一種方法。趣談NinjectHttpApplicationModule。我迷路了。

我應該如何修改我的NinjectMVC.cs和的Global.asax.cs文件嗎?我現在的東西粘貼在下面。


NinjectMVC3.cs

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

namespace MyApp.App_Start 
{ 
    using System.Reflection; 
    using Microsoft.Web.Infrastructure.DynamicModuleHelper; 
    using Ninject; 
    using Ninject.Web.Mvc; 
    using TennisClub.Configuration; 

    public static class NinjectMVC3 
    { 
     private static readonly Bootstrapper bootstrapper = new Bootstrapper(); 

     /// <summary> 
     /// Starts the application 
     /// </summary> 
     public static void Start() 
     { 
      DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule)); 
      DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule)); 
      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(); 
      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.Load(new MainModule()); // I added this 
     }   
    } 
} 

的Global.asax

namespace MyApp 
{ 
    public class MvcApplication : NinjectHttpApplication // new 
    { 
     protected override void OnApplicationStarted() // new 
     { 
      AreaRegistration.RegisterAllAreas(); 
      RegisterGlobalFilters(GlobalFilters.Filters); 
      RegisterRoutes(RouteTable.Routes); 
      ModelMetadataProviders.Current = new CustomModelMetadataProvider(); 
      ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new AttributedValidatorFactory())); 
      DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; 
     } 

     public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
     { 
      filters.Add(new HandleErrorAttribute()); 
     } 

     public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
      routes.MapRoute(
       "Default", // Route name 
       "{controller}/{action}/{id}", // URL with parameters 
       new { controller = Token.Home, action = Token.Index, id = UrlParameter.Optional }); // Parameter defaults 
     } 

     protected override IKernel CreateKernel() // new 
     { 
      return new StandardKernel(new MainModule()); 
     } 
    } 
} 

MainModule.cs

namespace MyApp.Configuration 
{ 
    public class MainModule : NinjectModule 
    { 
     public override void Load() 
     { 
      Bind<AppSettings>().ToSelf().InSingletonScope(); 
      Bind<MainDbContext>().ToSelf().InRequestScope(); 
      Bind<HttpContext>().ToMethod(context => HttpContext.Current); 
      Bind<UserInfo>().ToSelf().InRequestScope(); 
     } 
    } 
} 

回答

5

看一看這個網頁從維基GitHub上 -

https://github.com/ninject/ninject.web.mvc/wiki/Setting-up-an-MVC3-application

它經歷兩個不同的方法,並幫助我過去。

+0

好了,這聽起來像你說我應該只是恢復我的Global.asax到原來的形式和使用'App_Start.NinjectMVC3'類。但是什麼是'NinjectHttpApplicationModule'? – devuxer

+0

看起來你所引用的頁面比博客文章更近(這是我弄不清'NinjectHttpApplicationModule'),所以我想我會嘗試,現在。我有一些其他的事情弄清楚之前,我可以測試它是否工作正常,但+1對您有所幫助。 – devuxer