1

Why .Net WebApi don't detect the request contentType automatically and do auto-binding?

If I make a request without informing the contentType a HTTP 500 error occour:

No MediaTypeFormatter is available to read an object of type 'ExampleObject' from content with media type ''undefined''.

why not try to detect the incoming data and bind automatically?

Another case:

This request with Content-Type: application/x-www-form-urlencoded send a JSON:

User-Agent: Fiddler
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Host: localhost:10329
Content-Length: 42

Request Body:
{"Name":"qq","Email":"ww","Message":"ee"}:

My Action don't detect the JSON request data automatically in object param:

public void Create(ExampleObject example) //example is null
{
{

Instead of letting the object null why they do not try to solve it?

Then, for the binding occurs I need to send with Content-Type: application/json.

It would be best if .Net WebAPI detects the type of request data and do a auto-binding? Why not in this way?

5
  • You are specifying your content-type as form, and you want asp.net to override it? It seems like a very bad design decision to me. Commented Aug 12, 2012 at 20:53
  • @SergRogovtsev Instead of letting the object null why they do not try to solve it? And when I do not specify ContentType? Commented Aug 12, 2012 at 21:11
  • 6
    because that would be violating the intention of the sender, who specifically set data format. Commented Aug 12, 2012 at 21:21
  • 1
    @SergRogovtsev but the intention of the sender is post the data regardless of format, no? The data format will not matter in the end, the data need to be posted, that's the intention. Commented Aug 13, 2012 at 18:53
  • The data format does matter. If it doesn't, you should not have specified it in the first place. Commented Aug 13, 2012 at 21:13

2 Answers 2

1

application/x-www-form-urlencoded means you will be sending data in the x-www-form-urlencoded standard. Sending data in another standard will not work.

Sign up to request clarification or add additional context in comments.

2 Comments

Instead of letting the object null why they do not try to solve it? And when I do not specify ContentType?
One of the main philosophies behind the design of ASP.NET MVC is that the framework should adhere to the "standards of the internet". As a result, many aspects of the ASP.NET MVC infrastructure relies on the correct use of metadata such as ContentType and HTTP Verbs to function correctly.
1

Sounds like what you want to do is accept multiple formats from the server.

the way http works is that the client makes a request to the server for a resource and tells the server what content types it understands. This means that the client doesnt get a response it isnt able to decode, and the server knows which responses are more appropriate on the client. For example if you are a web-browser the most appropriate content type is text/html but if you get XML you can probably do something with that too. So you would make a request with the following:

   accept: text/html, application/xml

this says you prefer html but also understand XML

In your example if your client wants application/x-www-form-urlencoded but can also deal with JSON then you should do the following when making a request

 accept: application/x-www-form-urlencoded, application/json

For more details see the HTTP Spec on accept headers here http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

You may also want to create a new media type formatter so your server knows how to give clients application/x-www-form-urlencoded, take a look at this blog post for more info on how to do this http://www.strathweb.com/2012/04/rss-atom-mediatypeformatter-for-asp-net-webapi/

2 Comments

see the question. I'm not retrieving data, I'm posting data.
in webapi the same rules apply for post. The only difference is that instead of setting a set of accept headers you specify a single content-type header which tells the server the type of the posted data. if the server doesnt know how to decode the data (ie it doenst have a media type formatter for the content type) http says it should throw a 400 (bad request).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.