Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

请教.NET POST为空

我是.NET core初学者,现在遇到个奇怪的问题,模拟的服务器接收到的POST永远为NULL,但是debug显示,接收操作是没问题的,因为进入了我设置的断点

private static Dictionary<string, AddRequest> DB = new Dictionary<string, AddRequest>(); // 用内存里的字典模拟数据库存储

    [HttpPost]
    public AddResponse Post([FromBody] AddRequest req)
    {

        AddResponse resp = new AddResponse();    // 这里直接把json转换为c#的对象了
        try 
        {
            //DB.Add(req.ID, req);
            resp.ID = req.ID;
            resp.message = "交易成功";
            resp.result = "S";   // 接口规定成功用S  失败用F
        }
        catch (Exception ex) 
        {
            Console.WriteLine(ex);
            resp.ID = "";
            resp.message = "交易失败";
            resp.result = "F";   // 接口规定成功用S  失败用F
        }
        return resp;
    }

我通过postman发送post数据到服务器,try这个断点已经确认接收到了,但是跟踪发现req永远为Null,虽然对象创建成功了,为什么没有接收到数据呢?
下面是我的对象创建类型,以及我post的json数据
`
public class AddRequest

{
    public string ID;    //用户ID
    public string depID;   // 部门ID
    public UserInfo[] userInfo;   // 用户信息  包含了名字,性别,年龄之类的     // 我可能是这里错误了,导致post的数据不对?
}
public class UserInfo
{
    public string name;
    public string age;
    public string sex;
}
public class AddResponse
{
    public string result;
    public string message;
    public string ID;
}

`
json数据,我是通过postman发送的
`
{

"ID": "1",
"depID": "1",
"userInfo": [{
        "name": "ww1",
        "age": "33",
        "sex": "男"
    },
    {
        "name": "ww3",
        "age": "63",
        "sex": "女"
    }
]

}
`
### 请高手帮我看一下问题,自己找了2天,也没看出来具体原因,谢谢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...