我刚刚将Ninject.MVC3 nuget包(v2.2.2.0)添加到我的ASP.NET MVC 3应用程序中。
我似乎以两种不同的方式设置了我的ASP.NET MVC 3应用程序:
NinjectHttpApplication
NinjectModule
方法中加载RegisterServices
现在我正试图理解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();
}
}
}
答案 0 :(得分:5)
从GitHub上的Wiki看一下这个页面 -
https://github.com/ninject/ninject.web.mvc/wiki/Setting-up-an-MVC3-application
它贯穿了两种不同的方法,并在过去帮助了我。