Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

c# - Web API2 NinjectWebCommon.cs do not appear

I am doing an Empty Web API in Visual Studio 2013 Framework 4.5. Obviously NinjectWebCommon.cs do not appear.

I installed via Nuget,

  • Ninject,

  • Ninject.Web.Common,

  • Ninject.MVC5,

  • Ninject.Web.Common.WebHost,

  • Ninject.Web.WebApi,

  • Ninject.web.WebApi.WebHost

    but NinjectWebCommon.cs still does not appear.

What else do I need to install?

Can I add that file manually?

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like the most recent Ninject.Web.Common.WebHost 3.3.0 NuGet package no longer includes the NinjectWebCommon.cs. Older versions, such as 3.2.0 do include this file.

Ninject.Web.Common.WebHost 3.3.0 provides a NinjectHttpApplication class you can derive from and use instead of the NinjectWebCommon.cs. The wiki documentation for Ninject does not seem to have been updated but it looks like using the NinjectHttpApplication is one documented approach, as shown below:

public class MvcApplication : NinjectHttpApplication
{
   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 = "Home",
               action = "Index",
               id = UrlParameter.Optional
           });
   }

   protected override 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 void RegisterServices(IKernel kernel)
   {
       // e.g. kernel.Load(Assembly.GetExecutingAssembly());
   }

   protected override void OnApplicationStarted()
   {
       base.OnApplicationStarted();

       AreaRegistration.RegisterAllAreas();
       RegisterGlobalFilters(GlobalFilters.Filters);
       RegisterRoutes(RouteTable.Routes);
   }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...