asp.netjsonasp.net-mvcjil

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 ContentType:x-www-formencoded

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:

{"SessionToken":"65e2be91-a455-0ef3-0ba0-c2dd2c281ecc","ClientType":1,"OfferType":1,"DeviceInfo":{"Width":1080,"Height":1920}}

Now, in the MVC controller this is the method:

[HttpPost]
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.


Solution

  • 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