简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM