Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make sure that the controller has a parameterless public constructor

I get this error

An error occurred when trying to create a controller of type 'AnalyticController'. Make sure that the controller has a parameterless public constructor.

Code works in test environment but not on the production server.

Any idea what could cause the problem?

This is my controller

public class AnalyticController : ApiController
{
    private AnalyticBLL analyticBLL = new AnalyticBLL();

    // POST api/status
    public void Post(AnalyticDTO analyticDTO)
    {
        if (!analyticBLL.Register(analyticDTO))
            helpers.BusinessLayer.CreateResponseException(analyticBLL.Errors);   
    }
}
like image 918
GibboK Avatar asked Jun 03 '14 12:06

GibboK


4 Answers

I was getting this error because I had an Exception occurring in my parameter-ful constructor. It had nothing to do with requiring a parameter-less constructor despite the error message.

like image 130
user2444499 Avatar answered Sep 18 '22 01:09

user2444499


That depends on how you're handling DI (dependency injection). By default, controllers need paramaterless constructor, and you have to use something like Unity or Ninject to handle DI. I see you're inhereiting from ApiController, so if you're using Ninject make sure you download Ninject for Web API. If you already know everything I just told you, then you need to give more information. What's your setup? What are you using for DI?

like image 28
Pharylon Avatar answered Sep 21 '22 01:09

Pharylon


Add a public parameterless constructor and the problem goes away:

public class AnalyticController : ApiController
{
   public AnalyticController()
   {
   }

   // Variables and methods are the same as before
}
like image 26
Dominic Zukiewicz Avatar answered Sep 19 '22 01:09

Dominic Zukiewicz


Don't know if this will help anybody else. I build & then run my app against web services all on my local (dev) box. Then before I deploy to production, I run my app pointing to the existing production web services. This usually works, but this time, it worked "in dev" but gave me this "Make sure that the controller has a parameterless public constructor" error message "in production" (well, not really in production but against the production web services).

None of these solutions seemed like the problem for my code and when I deployed my code to production, I did not get this error when it is all in production. My guess is that it may have to do with different versions of the NuGet packages between my dev box & production. I'll investigate, I but just wanted to offer this up in case others have a similar "works against dev but not prod" situation. It may be an environment issue (depending on what you mean by does not work "in prod").

like image 22
rsmith Avatar answered Sep 21 '22 01:09

rsmith