Guy Assaf Guy Assaf - 1 year ago 134
ASP.NET (C#) Question

Jil serializer as MVC default, don't accept "json" ContentType

Does any one know if Jil can accept

ContentType: 'application/json'
in the http call to the API?

From what I see it can only accept

An example of what don't work for me, the object received in the controller is null.
This is the JS call

var request = $.ajax({
url: uri,
type: commad,
data: JSON.stringify(obj),
dataType: "json",
contentType: 'application/json',

This is the obj content:


Now, in the MVC controller this is the method:

public Task<ActionResult> GetUserOffers([FromBody]OffersRequestInfo obj)
if (obj == null)
return null;

CampaignLogic logic = new CampaignLogic();
Task<ActionResult> res = logic.GetOffers(obj);
return res;

the obj parameter received as null when using Jil, with Newtonsoft it is holding the value from the request.

Answer Source

The next line should be added to the constractor of the JilFormatter

  SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));

More explanation on how to implement Jil as the default mvc serializer: Here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download