ASP.NET的WebAPI JSON绑定的区分大小写 [英] ASP.NET WebAPI JSON Binding Case-Sensitivity

查看:404
本文介绍了ASP.NET的WebAPI JSON绑定的区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从ASP.NET的WebAPI测试版升级到RC提供了兴奋的一定量和挫折很大。我已经能够通过多数人的问题需要解决,但就是咬我现在一个是JSON请求数据的区分大小写。

Upgrading from ASP.NET WebAPI Beta to RC has provided some amount of excitement and a great deal of frustration. I have been able to work through the majority of the issues, but the one that is biting me right now is case-sensitivity of JSON request data.

使用JSON请求(默认情况下是一个JSON.NET格式化)格式化出现而用于表单urlen codeD数据格式化不区分大小写。有没有配置JSON请求使用不区分大小写格式的方法?

The formatter used for JSON requests (which by default is a JSON.NET formatter) appears to be case sensitive while the formatter used for form-urlencoded data does not. Is there a way to configure the JSON requests to use a case-insensitive formatter?

下面是一个简单的例子来说明,我用JSON的请求所遇到的问题:

Here is a simple example to illustrate the problem that I am having with JSON requests:

HTML / JavaScript的

<button id="tester">Click here!</button>

<script type="text/javascript">
    $(function () {
        $("#tester").on("click", function() {
            $.ajax({
                type: "POST",
                url: "/Api/Test/Index/" + 168,
                data: ko.toJSON({ key: 123, value: "test value" }), // THIS FAILS
                               // Key: 123, Value: "test value" <- BUT THIS WORKS
                contentType: "application/json; charset=utf-8",
                statusCode: {
                    200: function () {
                        $("body").append("<p>Success</p>");
                    },
                    400: function () {
                        $("body").append("<p>Failure</p>");
                    }
                }
            }).always(function () {
                $("body").append("<hr />");
            });
        });
    });
</script>

C#

public class TestController : ApiController
{
    public HttpResponseMessage Index(int? id, KeyValuePair<int, string> test)
    {
        if (id != 168 || test.Key != 123 || test.Value != "test value")
            return Request.CreateResponse(HttpStatusCode.BadRequest);

        return Request.CreateResponse(HttpStatusCode.OK);
    }
}

我提供的,其中提供了JSON数据行的注释。我宁愿不通过适当的外壳财产成员与我的JavaScript对象打破惯例,我当然不希望低打破惯例套管我的C#属性。有什么想法?

I've provided a comment on the line where the JSON data is provided. I would rather not break convention with my JavaScript objects by proper casing property members and I certainly do not want to break convention by lower casing my C# properties. Any thoughts?

推荐答案

挖掘到这个问题的深入显著后,我才明白,我打了Json.NET KeyValuePair器的错误。詹姆斯·牛顿国王还跟我回答相关的问题,并提供了一​​个链接到修复:

After digging into this issue in significant depth I came to realize that I was hitting a bug in the Json.NET KeyValuePair converter. James Newton-King was kind enough to answer my related question and provided a link to the fix:

<一个href=\"http://stackoverflow.com/questions/11266695/json-net-case-insensitive-property-deserialization\">Json.NET不区分大小写的属性反序列化

这篇关于ASP.NET的WebAPI JSON绑定的区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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