[英]Configuring Ninject in App_start folder specifically ninjectwebcommon.cs file
我有一個簡單的查詢,我正在關注此鏈接上的教程: http : //www.prideparrot.com/blog/archive/2012/12/how_to_create_a_simple_blog_part1#book-unique-identifier我的問題是該教程的作者configure ninject在global.asax文件中,並刪除了ninjectwebcommon.cs文件。 我正在嘗試將justblog集成到使用ninjectwebcommon.cs文件的現有asp.netMVC5應用程序中。
任何幫助將非常感激。
您是否使用Nuget添加Ninject? 您需要對WebActivatorEx
的引用才能使引導程序正常工作(顯然,還要與其他必需的Ninject引用一起使用)。 在項目的App_Start
文件夾中添加一個NinjectWebCommon.cs類,如下所示:
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(YourMvcApp.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(YourMvcApp.App_Start.NinjectWebCommon), "Stop")]
namespace YourMvcApp.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(); // you'll add modules to the parameter list here
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
//RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
///// <summary>
///// Load your modules or register your services here!
///// </summary>
///// <param name="kernel">The kernel.</param>
//private static void RegisterServices(IKernel kernel)
//{
//}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.