我有一个.NET Core Web API,在尝试将包含一些json数据的数据发布到API时,返回了415 Unsupported Media Error。以下是在Chrome调试器中返回的部分内容:
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
装饰我的控制器,但似乎没有起作用。
[HttpPost]
[Route("api/trackAllInOne/set")]
[Consumes("application/x-www-form-urlencoded")]
public IActionResult Post([FromBody] PlayerPackage playerPackage)
{ etc..}
非常感激任何帮助。
以下代码在 .NET 4.6.1 中运行良好,我能够捕获并处理上面显示的帖子。
[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
?你是如何从前端发送请求的? - Edwarddata:{"status":"incomplete","score":""}
的数据还是记录所有的主体action:finish currentSco:CSharp-SSLA:__How_It_Works_SCO data:{"status":"incomplete","score":""} activityId:13 studentId:1 timestamp:1519864867900
? - Edwardvar id = Convert.ToInt32(playerPackage.ActivityId); var learningRecord = await _context.LearningRecords.FindAsync(id); if (learningRecord == null) return NotFound();`
- Roddy Balkan