Post FromBody始终无效 [英] Post FromBody Always Null

查看:496
本文介绍了Post FromBody始终无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个新的API,我正在使用ASP.NET Core构建,我无法将任何数据POST到端点。

I've got a new API that I'm building with ASP.NET Core, and I can't get any data POST'ed to an endpoint.

以下是端点的样子:

[HttpPost]
[Route("StudentResults")]
public async Task<IActionResult> GetStudentResults([FromBody]List<string> userSocs, [FromBody]int collegeId)
{
    var college = await _collegeService.GetCollegeByID(collegeId);
    // var occupations = await _laborMarketService.GetOccupationProgramsBySocsAndCollege(userSocs, college);
    return Ok();
}

这就是我通过Postman发送的有效载荷如下:

And here's what my payload that I'm sending through Postman looks like:

{
    "userSocs": [
            "291123",
            "291171",
            "312021",
            "291071",
            "152031",
            "533011"
        ],
    "collegeId": 1
}

我确保我将邮递员设为POST, Content-Type application / json 。我究竟做错了什么?

I'm making sure that I have postman set as a POST, with Content-Type application/json. What am I doing wrong?

推荐答案

你总是 null 因为你需要封装你的所有帖子只有一个对象内的变量。像这样:

You get always null because you need to encapsulate all your post variables inside only one object. Like this:

public class MyPostModel {
    public List<string> userSocs {get; set;}
    public int collegeId {get; set;}
}

然后

public async Task<IActionResult> GetStudentResults([FromBody] MyPostModel postModel)

这篇关于Post FromBody始终无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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