Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET WebAPI JSON Binding Case-Sensitivity

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.

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?

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);
    }
}

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?

like image 563
Phil Klein Avatar asked Jun 29 '12 14:06

Phil Klein


People also ask

Is JSON API case sensitive?

JSON is case-sensitive. SQL is case-insensitive, but names in SQL code are implicitly uppercase.

Are Web API routes case sensitive?

HTTP MethodsBy default, Web API looks for a case-insensitive match with the start of the controller method name.

Should a REST API be case sensitive or non case sensitive?

Rest API parameter are case-sensitive, Like User_Id and user_id are not interchangeable.

Is model binding case sensitive?

Model Binding in controller methods is case insensitive.


2 Answers

The Json.NET formatter is case insensitive for deserialization (json -> CLR).

On serialization, you can get camel casing by using the CamelCasePropertyNamesContractResolver.

In your Global.asax:

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
like image 51
Mike Wasson Avatar answered Oct 05 '22 23:10

Mike Wasson


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:

Json.NET Case-insensitive Property Deserialization

like image 26
Phil Klein Avatar answered Oct 06 '22 00:10

Phil Klein