如果是大小写敏感的JSON请求到ASP.NET Web服务(ASMX)重要? [英] When is case sensitivity important in JSON requests to ASP.NET web services (ASMX)?

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

问题描述

我已经做了使用JSON请求下面的测试发送到ASP.NET 2.0 ASMX Web服务(使用AJAX扩展1.0 ASP.NET 2.0),它似乎是区分大小写是在某些情况下很重要,但不是在其他人。请参见下面的例子:

  • 案例匹配100%:

      {请求:{地址:{地址1:123大街,地址2:一套20,城市:纽约 国:纽约,邮编:10000,AddressClassification:空}}}
     

    结果: HTTP / 1.1 200 OK

  • 包含对象名称的案例地址不匹配:

      {请求:{地址:{地址1:123大街,地址2:一套20,城市:纽约 国:纽约,邮编:10000,AddressClassification:空}}}
     

    结果: HTTP / 1.1 200 OK

  • Web服务参数的案例要求不匹配:

      {请求:{地址:{地址1:123大街,地址2:一套20,城市:纽约 国:纽约,邮编:10000,AddressClassification:空}}}
     

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

(快速注:事实上,类要求和参数要求共享同一个名称是不是初步认识。即使我更改参数名称 lrequest ,区分大小写仍然是必需的。)

当是JSON Web服务请求的区分大小写重要?同时,这是一个普遍的Web服务的问题,或者这是针对ASP.NET AJAX?


其他背景资料:

我使用了AJAX扩展1.0 ASP.NET 2.0,所以这可能会在以后版本的框架中得到解决。如果是的话,请让我知道。

跟进的答案在我最近的<经过href="http://stackoverflow.com/questions/2737525/how-do-i-build-a-json-object-to-send-to-an-ajax-webservice">question关于,我意识到,我的要求是失败的原因是因为无效JSON格式不JSON字符串(感谢 TJ克劳德的指出了这一点,并链接到 http://www.jsonlint.com/ 以JSON验证)。相反,做一些更多的测试后,我才知道,这个问题是因为Web服务做了我的JSON对象没有被格式化方式,我发现Web服务是非常的挑剔的问候,以区分大小写。似乎有时区分大小写是重要的,而其他时候,它是不(见上面的例子)。

下面是我的C#code为Web方法和类的样子:

  [WebMethod的]
公众的反应对validateAddress(申请要求)
{
    返回新test_AddressValidation()。GenerateResponse(
        test_AddressValidation.ResponseType.Ambiguous);
}

...

公共类请求
{
    公共广播地址;
}

公共类地址
{
    公共字符串地址1;
    公共字符串地址2;
    公共字符串市;
    公共字符串的国家;
    公共字符串邮编;
    公共AddressClassification AddressClassification;
}

公共类AddressClassification
{
    公众诠释code;
    公共字符串描述;
}
 

解决方案

据<一href="http://jsonrpc.org/historical/json-rpc-1-1-alt.html#service-procedure-and-parameter-names">JSON-RPC SPEC ,答案永远是。

  

6.5。区分大小写程序和参数名称

     

顺应的实现必须治疗   程序和参数名称作为   区分大小写的,例如姓名栏和   BAR将被视为两个不同的   实体。

因此​​,它听起来就像情况下,当它的工作对你是例外,不是他们没了的情况下。很可能有人在等式的一些副作用只是不遵守的规范。

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:

  • Case matches 100%:

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

    Result: HTTP/1.1 200 OK

  • 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}}}
    

    Result: HTTP/1.1 200 OK

  • 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}}}
    

    Result: HTTP/1.1 500 Internal Server Error

(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.)

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?


Additional background information:

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.

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).

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

解决方案

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

6.5. Case-Sensitivity of Procedure and Parameter Names

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.

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

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