mardi 24 avril 2018

web api controller only contain empty constructor

Let me describe the sitation.

I use web api and in the controller class where I define my http verbs I noticed that I cannot create a constructor. infact if I add a constructor which is empty it is oke but when I modify this constructor it is not. It gives me the following error when I try to post with postman to the method inside the controller class

{
"Message": "An error has occurred.",
"ExceptionMessage": "An error occurred when trying to create a controller of type 'DataController'. Make sure that the controller has a parameterless public constructor.",
"ExceptionType": "System.InvalidOperationException",
"StackTrace": "   bij System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n   bij System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n   bij System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()",
"InnerException": {
    "Message": "An error has occurred.",
    "ExceptionMessage": "Type Chatbot.Controller.DataController heeft geen standaardconstructor",
    "ExceptionType": "System.ArgumentException",
    "StackTrace": "   bij System.Linq.Expressions.Expression.New(Type type)\r\n   bij System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n   bij System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n   bij System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"
}

}

the controller class

 public class DataController : ApiController {
    public string data { get; set; }
    public DataController(string analyticsData) {
        data = analyticsData;
    }

    public void Post([FromBody] dynamic data) {
       string value = data.value;

How can I achieve my goal to add a non empty constructor to the controller class without resulting in this error?




Aucun commentaire:

Enregistrer un commentaire