0
votes

' 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?

1
See the answers to this question: stackoverflow.com/questions/14407458/…Alireza

1 Answers

0
votes

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 "";              
    }