接受Web API .Net Core中的x-www-form-urlencoded [英] Accept x-www-form-urlencoded in Web API .Net Core

查看:970
本文介绍了接受Web API .Net Core中的x-www-form-urlencoded的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.Net Core Web API,当我尝试向其中发布包含一些json的数据时,返回415不支持的媒体错误.这是Chrome调试器返回的内容的一部分:

I have a .Net Core Web API that is returning a 415 Unsupported Media Error when I try to post some data to it that includes some json. Here's part of what is returned in the Chrome Debugger:

Request URL:http://localhost:51608/api/trackAllInOne/set
Request Method:POST
Status Code:415 Unsupported Media Type
Accept:text/javascript, text/html, application/xml, text/xml, */*
Content-Type:application/x-www-form-urlencoded

action:finish
currentSco:CSharp-SSLA:__How_It_Works_SCO
data:{"status":"incomplete","score":""}
activityId:13
studentId:1
timestamp:1519864867900

我认为这与我的控制器不接受application/x-www-form-urlencoded数据有关-但我不确定.我尝试用Consumes装饰我的控制器,但这似乎不起作用.

I think this has to do with my controller not accepting application/x-www-form-urlencoded data - but I'm not sure. I've tried decorating my controler with Consumes but that does not seem to work.

[HttpPost]
[Route("api/trackAllInOne/set")]
[Consumes("application/x-www-form-urlencoded")]
public IActionResult Post([FromBody] PlayerPackage playerPackage)
{ etc..}

任何帮助,我们将不胜感激.

Any help greatly appreciated.

以下代码在.Net 4.6.1中工作正常,我能够捕获和处理上面显示的帖子.

The following code worked fine in .Net 4.6.1 and I am able to capture and process the posts shown above.

[ResponseType(typeof(PlayerPackage))]
public async Task<IHttpActionResult> PostLearningRecord(PlayerPackage playerPackage)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        var id = Convert.ToInt32(playerPackage.ActivityId);
        var learningRecord = await _context.LearningRecords.FindAsync(id);
        if (learningRecord == null)
            return NotFound();
etc...

推荐答案

对于PlayerPackage,请求应发送一个 PlayerPackage Json对象,根据您的描述,您无法控制该请求从其他地方发布.

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.

对于请求,其类型为 application/x-www-form-urlencoded ,它将发送带有 {"status":"incomplete","score":"的数据} ,而不是Json对象.如果您想接受 {"status":"incomplete","score":"} ,建议您像下面那样更改方法,然后通过Newtonsoft.Json将字符串转换为Object

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);
    }

这篇关于接受Web API .Net Core中的x-www-form-urlencoded的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆