Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC5, Web API 2 and Ninject
    primarykey
    data
    text
    <p>I have created a new MVC5 project with Web API 2, I then added the Ninject.MVC3 package from NuGet.</p> <p>Constructor injection is working fine for the MVC5 controllers, but i am getting an error when trying to use it with the Web API Controllers.</p> <blockquote> <p>An error occurred when trying to create a controller of type 'UserProfileController'. Make sure that the controller has a parameterless public constructor.</p> </blockquote> <p>Constructor for working MVC5 controller:</p> <pre class="lang-cs prettyprint-override"><code>public class HomeController : Controller { private IMailService _mail; private IRepository _repo; public HomeController(IMailService mail, IRepository repo) { _mail = mail; _repo = repo; } } </code></pre> <p>Constructor for non-working Web API Controller:</p> <pre class="lang-cs prettyprint-override"><code>public class UserProfileController : ApiController { private IRepository _repo; public UserProfileController(IRepository repo) { _repo = repo; } } </code></pre> <p>Below is the full NinjectWebCommon.cs file:</p> <pre class="lang-cs prettyprint-override"><code>[assembly: WebActivator.PreApplicationStartMethod(typeof(DatingSite.App_Start.NinjectWebCommon), "Start")] [assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(DatingSite.App_Start.NinjectWebCommon), "Stop")] namespace DatingSite.App_Start { using System; using System.Web; using Microsoft.Web.Infrastructure.DynamicModuleHelper; using Ninject; using Ninject.Web.Common; using DatingSite.Services; using DatingSite.Data; public static class NinjectWebCommon { private static readonly Bootstrapper bootstrapper = new Bootstrapper(); /// &lt;summary&gt; /// Starts the application /// &lt;/summary&gt; public static void Start() { DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); bootstrapper.Initialize(CreateKernel); } /// &lt;summary&gt; /// Stops the application. /// &lt;/summary&gt; public static void Stop() { bootstrapper.ShutDown(); } /// &lt;summary&gt; /// Creates the kernel that will manage your application. /// &lt;/summary&gt; /// &lt;returns&gt;The created kernel.&lt;/returns&gt; private static IKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Bind&lt;Func&lt;IKernel&gt;&gt;().ToMethod(ctx =&gt; () =&gt; new Bootstrapper().Kernel); kernel.Bind&lt;IHttpModule&gt;().To&lt;HttpApplicationInitializationHttpModule&gt;(); RegisterServices(kernel); return kernel; } /// &lt;summary&gt; /// Load your modules or register your services here! /// &lt;/summary&gt; /// &lt;param name="kernel"&gt;The kernel.&lt;/param&gt; private static void RegisterServices(IKernel kernel) { #if DEBUG kernel.Bind&lt;IMailService&gt;().To&lt;MockMailService&gt;().InRequestScope(); #else kernel.Bind&lt;IMailService&gt;().To&lt;MailService&gt;().InRequestScope(); #endif kernel.Bind&lt;SiteContext&gt;().To&lt;SiteContext&gt;().InRequestScope(); kernel.Bind&lt;IRepository&gt;().To&lt;Repository&gt;().InRequestScope(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload