简体   繁体   English

HTTP Post请求未针对JSON哈希正确反序列化

[英]HTTP Post request is not de-serializing correctly for JSON Hash

My ASP.NET MVC action looks like this: 我的ASP.NET MVC操作如下所示:

public ActionResult Create(APIRequest request)
{
}

My class looks like: 我的课看起来像:

public class APIRequest
{
    public Dictionary<string, string> Atts { get; set; } 

    public APIRequest()
    {

    }
}

On the front-end I am sending the AJAX call like so: 在前端,我像这样发送AJAX调用:

 var atts = {
        'userId' : '10203',
        'foo': '1',
        'bar': '2',
        'some-invalid-identifier': '3'
    };



 $.ajax({
        url: "/path/to/save",
        type: "POST",
        data: JSON.stringify(atts),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function(r) {

        },
        success: function(r) {

        }
    });

When I set a breakpoint in my controller, the request variable Atts (dictionary) property is always null. 当我在控制器中设置断点时,请求变量Atts(字典)属性始终为null。

What is wrong here? 怎么了

Your json is not providing the Atts property. 您的json不提供Atts属性。 The way you are sending it, the model binder will be looking for 4 properties - userId, foo, bar and some-invalid-identifier. 发送方式,模型绑定器将查找4个属性-userId,foo,bar和some-invalid-identifier。 This will work: 这将起作用:

{
   "Atts" : { "userId" : "10203",
              "foo": "1",
              "bar": "2",
              "some-invalid-identifier": "3"
            }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM