对 ASP.NET Web 服务 (ASMX) 的 JSON 请求中区分大小写何时重要? [英] When is case sensitivity important in JSON requests to ASP.NET web services (ASMX)?

查看:18
本文介绍了对 ASP.NET Web 服务 (ASMX) 的 JSON 请求中区分大小写何时重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对发送到 ASP.NET 2.0 ASMX Web 服务的 JSON 请求(使用 AJAX Extensions 1.0 for ASP.NET 2.0)进行了以下测试,似乎区分大小写在某些情况下很重要,但在其他情况下则不重要.请参阅以下示例:

I've done the following tests with JSON requests sent to an ASP.NET 2.0 ASMX web service (using AJAX Extensions 1.0 for ASP.NET 2.0) and it seems that case sensitivity is important in some situations but not in others. See the following examples:

  • 大小写匹配 100%:

  • Case matches 100%:

{"request":{"Address":{"Address1":"123 Main Street","Address2":"suite 20","City":"New York","State":"NY","Zip":"10000","AddressClassification":null}}}

结果:HTTP/1.1 200 OK

包含对象名称Address的大小写不匹配:

Case of contained object name Address does not match:

{"request":{"address":{"Address1":"123 Main Street","Address2":"suite 20","City":"New York","State":"NY","Zip":"10000","AddressClassification":null}}}

结果:HTTP/1.1 200 OK

Web 服务参数request 不匹配的情况:

Case of web service parameter request does not match:

{"Request":{"address":{"Address1":"123 Main Street","Address2":"suite 20","City":"New York","State":"NY","Zip":"10000","AddressClassification":null}}}

结果:HTTP/1.1 500 内部服务器错误

Result: HTTP/1.1 500 Internal Server Error

(快速说明:类 Request 和参数 request 共享相同名称的事实并不相关.即使我将参数名称更改为lrequest,仍然需要区分大小写.)

(Quick note: The fact that the class Request and the parameter request share the same name is not relavant. Even if I change the parameter name to lrequest, case sensitivity is still required.)

JSON Web 服务请求中的区分大小写何时重要?此外,这是一个通用的 Web 服务问题还是特定于 ASP.NET AJAX?

When is case sensitivity in JSON Web Service requests important? Also, is this a general web service issue or is this specific to ASP.NET AJAX?

其他背景信息:

我正在为 ASP.NET 2.0 使用 AJAX 扩展 1.0,所以这可能在框架的更高版本中得到解决.如果是这样,请告诉我.

I'm using the AJAX Extensions 1.0 for ASP.NET 2.0, so this may have been addressed in later versions of the framework. If so please let me know.

在跟进我最近的答案之后 关于格式化 JSON 字符串的问题,我意识到我的请求失败的原因不是因为无效的 JSON(感谢 TJ Crowder 指出并链接到 http://www.jsonlint.com/ 用于 JSON 验证).相反,在进行了更多测试之后,我了解到问题在于 Web 服务并没有按照我的 JSON 对象的格式设置格式,而且我发现 Web 服务在区分大小写方面非常挑剔.似乎有时区分大小写很重要,而有时则不重要(请参见上面的示例).

After following up to the answers in my most recent question regarding formatting JSON strings, I realized that the reason my request was failing wasn't because of invalid JSON (thanks to T.J. Crowder for pointing that out and linking to http://www.jsonlint.com/ for JSON validation). Rather, after doing some more testing, I learned that the problem was because the web service didn't how my JSON object was formatted and I discovered the web service was very picky in regards to case sensitivity. It seems that sometimes case sensitivity is important, whereas other times it is not (see examples above).

这是我的 Web 方法和类的 C# 代码的样子:

Here's what my C# code for the web method and classes looks like:

[WebMethod]
public Response ValidateAddress(Request request)
{
    return new test_AddressValidation().GenerateResponse(
        test_AddressValidation.ResponseType.Ambiguous);
}

...

public class Request
{
    public Address Address;
}

public class Address
{
    public string Address1;
    public string Address2;
    public string City;
    public string State;
    public string Zip;
    public AddressClassification AddressClassification;
}

public class AddressClassification
{
    public int Code;
    public string Description;
}

推荐答案

根据 JSON-RPC 规范,答案总是如此.

According to JSON-RPC spec, the answer is always.

9.0 过程和参数名称区分大小写

9.0 Case-Sensitivity of Procedure and Parameter Names

符合要求的实现必须处理过程和参数名称为区分大小写,例如名称栏和BAR 将被视为两个不同的实体.

Conforming implementations MUST treat procedure and parameter names as being case-sensitive such the names bar and BAR would be seen as two distinct entities.

所以,听起来它对您有用的情况是例外,而不是它们不起作用的情况.很可能有人在等式的某个方面只是不遵守规范.

So, it sounds like the situations when it worked for you were the exceptions, not the cases where they didn't. Chances are someone on some side of the equation was just not adhering to the specs.

这篇关于对 ASP.NET Web 服务 (ASMX) 的 JSON 请求中区分大小写何时重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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