16

I'm not entirely sure whats happened here. I may have messed things up somewhere, but I don't know what.

My API controller method looks like this:

public HttpResponseMessage<string> Put(int id)

I've tried a string as well with the same error.

Any ideas?

Thanks.

Edit: To be clear - the id is the route parameter. The body of the request is JSON. If I remove the route parameter,the method functions as normal.

Roberto Bonini
  • 7,164
  • 6
  • 39
  • 47

5 Answers5

13

Surprisingly, int and string do not have a MediaTypeFormatter by default, so it doesn't know how to handle those types.

The only types it knows how to handle out of the box are JSON, XML, and form url-encoded data. This quote is from the official asp.net website, http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters

In Web API, the media type determines how Web API serializes and deserializes the HTTP message body. There is built-in support for XML, JSON, and form-urlencoded data, and you can support additional media types by writing a media formatter.

Now you 'can' write your own MediaTypeFormatter (the link I provided will show you how), but since the asp.net web api is still in beta I have had a lot of trouble with it using custom formatters for simple types like strings. I found it is much easier just to wrap whatever value you would like to PUT in xml / json and it will automatically get deserialized. See my post here for more info on that, When HTTP-POST has body, url parameter is null

For your specific example your PUT body would look like,

<message>
   <id>6</id>
</message>

Then be sure to set the content-type of your http request to text/xml (or application/json if you choose to use that). And it should serialize just fine into the variable.

Community
  • 1
  • 1
Despertar
  • 21,627
  • 11
  • 81
  • 79
  • This would apply if it were being passed in the body - my issue here is with the route parameter itself - in this case, id. – Roberto Bonini Apr 03 '12 at 14:52
  • Yes, this appears to be a bug in asp.net web api beta. When you have a body in a PUT or POST the url parameter will be null. The only alternative I can offer for the time being is to pass everything in the body, however I know this may compromise your RESTful url structure which is important. – Despertar Apr 03 '12 at 16:12
  • I think this should be the accepted answer. The current accepted answer addresses the issue but doesn't have the solution. This answer does have the solution. – John Livermore Jun 20 '17 at 15:24
7

Are you placing the int value in the body of the request message? If so, what is the format?

If the content type is text, Web API won't know what to do with it, because Web API does not provide a media formatter for text. You can write a custom formatter, as Despertar noted.

You can also do something like this:

public void Put()
{
    var s = Request.Content.ReadAsStringAsync().Result;
}
Mike Wasson
  • 6,572
  • 2
  • 24
  • 20
  • The body is JSON. I just need it to work with a route parameter. If i remove the route parameter from the method definition, it works. – Roberto Bonini Apr 03 '12 at 14:44
6

Please read following blog post:

This describes most typical issues with using simple parameters. Without knowing any more details about how your request looks it is not possible to determine which one you have hit.

UPDATE

There is one more known bug considering route values. In case when not one of built in formatters is used for POST/PUT/PATCH request, the route values parameters are not being bind. To work around this the best solution is to write ActionFilterAttribute as described below:

tpeczek
  • 23,867
  • 3
  • 74
  • 77
0

On a side note...I have seen it throw this error when the service started requiring 'HTTPS'.

Prisoner ZERO
  • 13,848
  • 21
  • 92
  • 137
-1

I posted a DELETE with a json body and received this. Posting with parameters resolved the problem.

Ken
  • 1