Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FormUrlEncodedMediaTypeFormatter vs JQueryMvcFormUrlEncodedFormatter

I have created a default web api project with at least those 2 media formatters loaded. Both are for the same content-type:

FormUrlEncodedMediaTypeFormatter: application/x-www-form-urlencoded
JQueryMvcFormUrlEncodedFormatter: application/x-www-form-urlencoded

When I do a simple http post form with enctype="application/x-www-form-urlencoded" it works only with the JQueryMvcFormUrlEncodedFormatter, that means my sent complex object is not null at server side.

When I remove the formatter JQueryMvcFormUrlEncodedFormatter at application startup and do the simple http post form again I expect it to work again but it does not.

I get an exception that no appropriate formatter is loaded.

Thats not true -actually-

Why does it not work?

P.S.

I found that this is the difference:

– System.Net.Http.Formatting.FormUrlEncodedMediaTypeFormatter, for handling HTML form URL-encoded data
– System.Web.Http.ModelBinding.JQueryMvcFormUrlEncodedFormatter, for handling model-bound HTML form URL-encoded data

but I do not understand the difference!

I do not even use jquery to post my form:

<form role="form" method="post" action="api/values"    enctype="application/x-www-form-urlencoded">
    <input type="text" class="form-control" name="firstName" placeholder="Enter first name">
    <input type="text" class="form-control" name="lastName" placeholder="Enter last name">
    <button type="submit" class="btn btn-default">Submit</button>
</form>
like image 888
Pascal Avatar asked Mar 29 '16 17:03

Pascal


People also ask

What is the role of MediaTypeFormatter?

The Media type formatters are the classes that are responsible for serializing the request/response data so that the Web API Framework can understand the request data format and also send data in the format which the client expects.

What are Formatters in Web API?

Media type formatters are classes responsible for serializing request/response data so that Web API can understand the request data format and send data in the format which client expects. Web API includes following built-in media type formatters.

What are the advantages of Web API?

WEB API is a better choice for simpler, light weight services. WEB API can use any text format including XML and is faster than WCF. WEB API can be used to create full-blown REST Services. WEB API doesn't require any data contracts and doesn't require configurations to the level of WCF.


2 Answers

FormUrlEncodedMediaTypeFormatter binds application/x-www-form-urlencoded body to FormDataCollection type, and only that type.

JQueryMvcFormUrlEncodedFormatter however uses available ModelBinders to first parse the body to FormDataCollection and then use the first compatible ModelBinder to parse that into the final model. It's like mixing Model Binding approach and Media Type Formatting approach, which AFAIK is not explained anywhere in the WebAPI docs.

Both formatters are registered by default. This is what I inferred reading WebAPI source code.

like image 64
omittones Avatar answered Oct 09 '22 03:10

omittones


There are 4 out of box formatters

  • JsonMediaTypeFormatter
  • XMLMediaTypeFormatter
  • FormUrlEncodedMediaTypeFormatter
  • JQueryMvcFormUrlEncodedFormatter

    The first two media type formatters can serialize and deserialize CLR types to request/response and vice versa. But 3rd one neither serializes nor deserializes to/from any CLR type. 4th formatter can deserialize incoming raw data to CLR type.

    Hence in your case, you are experiencing with error after you remove JQueryMvcFormUrlEncodedFormatter since 3rd formatter could not able to deserialize incoming request data to CLR type.

like image 44
Rammohan Sonte Avatar answered Oct 09 '22 04:10

Rammohan Sonte