Make sure the controller has an immortal public constructor

I get this error

An error occurred while trying to create a controller of type AnalyticController. Make sure that the controller does not have a constructor without parameters.

The code works in a test environment, but not on a production server.

Any idea what might 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); } } 
+6
source share
4 answers

Add an open constructor without parameters and the problem will disappear:

 public class AnalyticController : ApiController { public AnalyticController() { } // Variables and methods are the same as before } 
+4
source

It depends on how you handle DI (dependency injection). By default, controllers need a constructor without parameters, and you should use something like Unity or Ninject to handle DI. I see that you are getting from ApiController, so if you are using Ninject, make sure you download Ninject for Web API . If you already know everything that I just told you, then you need to provide additional information. What is your setting? What do you use for DI?

+8
source

I was getting this error because I had an exception in my parameter constructor. This had nothing to do with the requirement of a constructor without parameters, despite the error message.

+2
source

I don't know if this will help anyone else. I create and then run my application against web services in a local (dev) window. Then, before I go into production, I launched an application that points to existing web services. Usually this works, but this time it worked "in dev", but gave me this "Make sure that the controller has an error message" without parameters "during production" (well, really, not in production, but in relation to production web services).

None of these solutions seemed to be a problem for my code, and when I deployed my code for production, I did not get this error when everything is ready. I guess this could be due to the different versions of NuGet packages between my development box and production. I will investigate, but I just wanted to suggest this in case others have a similar situation of “works against dev, not prod”. This may be a problem with the environment (depending on what you mean by “not working”).

0
source

Source: https://habr.com/ru/post/970238/


All Articles