2014-04-29 75 views
1

我有一個簡單的查詢,我下面這個鏈接教程: http://www.prideparrot.com/blog/archive/2012/12/how_to_create_a_simple_blog_part1#book-unique-identifier 我的問題是關於在Global.asax文件本教程配置ninject作者和刪除ninjectwebcommon.cs文件。我正在嘗試將justblog集成到使用ninjectwebcommon.cs文件的現有asp.netMVC5應用程序中。配置Ninject在App_start文件夾中專門ninjectwebcommon.cs文件

任何幫助將不勝感激。

+0

您面臨的確切問題是什麼?可以詳細說明 – qamar

+0

@qamar am遵循這個http://www.prideparrot.com/blog/archive/2012/12/how_to_create_a_simple_blog_part1#book-unique-identifier – tmcpete

+0

你可以在你的global.asax app_start中創建一個ninjectwebcommon類的實例功能。它會被實例化,當應用程序啓動 – qamar

回答

1

您是否使用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) 
     //{ 
     //} 
    } 
}  
相關問題