2011-10-04 61 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