0

' trying to make an web api controller with two parameters: one model object and string.

public string AddDevice(Device device, [FromBody] string userName)
{
    // Some operations here               
}

When I try it with one parameter on fiddler: For Device object (body request):

{
    "DeviceName":"name,
    "StolenFlag":false
}

For string "[FromBody] string userName" (body request):

"userName"

It works fine. I just do not know how to make this method works with those two parameters. When I try connecting request body on fiddler like that:

{
    "DeviceName":"name,
    "StolenFlag":false
}
"userName"

I get an 500 error. It means, that server finds correct controller method but can't handle request. Any ideas?

laik
  • 111
  • 13
  • See the answers to this question: http://stackoverflow.com/questions/14407458/webapi-multiple-put-post-parameters – Alireza Feb 10 '16 at 23:03

1 Answers1

0

First add the following line to WebApiConfig.cs in your App_Start folder.

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

It goes inside this function:

public static void Register(HttpConfiguration config)

Build your API and read the full error message from the Response. This should tell you exactly what's happening.

Since you can have only one parameter in the Request body you can change the method to accept username in the URI.

   public string AddDevice([FromBody] Device device, string userName)
    {
        // Some operations here
        return "";              
    }
jsxsl
  • 200
  • 8
  • {"Message":"An error has occurred.","ExceptionMessage":"Can't bind multiple parameters ('device' and 'userName') to the request's content.","ExceptionType":"System.InvalidOperationException","StackTrace":" w System.Web.Http.Controllers.HttpActionBinding.ExecuteBindingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)\r\n w System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext() – laik Feb 10 '16 at 23:08
  • So like I told you, theres problem with binding two params. Am I doing it right? The body request is in first post. – laik Feb 10 '16 at 23:09
  • If the username is a string can you put it in the URI instead of the body? You can only have one parameter in the body – jsxsl Feb 10 '16 at 23:14