Accept x-www-form-urlencoded in Web API .Net Core

前提是你 提交于 2019-11-30 19:24:34

For PlayerPackage, the request should send a PlayerPackage Json Object, based on your description, you could not control the request which is posted from other place.

For the request, its type is application/x-www-form-urlencoded, it will send data with {"status":"incomplete","score":""} in string Format instead of Json object. If you want to accept {"status":"incomplete","score":""}, I suggest you change the method like below, and then conver the string to Object by Newtonsoft.Json

    [HttpPost]
    [Route("~/api/trackAllInOne/set")]
    [Consumes("application/x-www-form-urlencoded")]
    public IActionResult Post([FromForm] string data)
    {
        PlayerPackage playerPackage = JsonConvert.DeserializeObject<PlayerPackage>(data);
        return Json(data);
    }

Try using [FromForm] instead of [FromBody]

public IActionResult Post([FromForm] PlayerPackage playerPackage)

FromBody -> If you binding from JSON

FromForm -> If you binding from Form parameters

NOTE 1:

You can also remove [FromBody] altogether and trial it then. Because you are expecting form-urlencoded should tell it to bind to object.

I had the same problem. FormDataCollection has no default constructors which is required by Formatters. Use IFormCollection instead

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!