0

我有以下合同:

[OperationContract]
[WebGet(UriTemplate = "Devices/{*id}", ResponseFormat = WebMessageFormat.Json)]
Device GetDevice(string id);

设备的 id 可以包含反斜杠“\”和“&”,因为它表示设备的路径。

当我发送带有 id 的 GET 请求时,GetDevice 函数接收到 id,但使用斜杠“/”而不是反斜杠“\”。

有什么办法可以防止这种情况?

4

1 回答 1

2

在此处查看这篇文章,了解如何处理传递给 WCF 服务的实体键中的特殊字符。

在您的情况下需要注意的是,关闭请求过滤并不能防止反斜杠被破坏:

<httpRuntime requestPathInvalidCharacters="" requestValidationMode="2.0"/> 
<pages validateRequest="false"/>

斜杠和问号是有问题的,因为底层的 URI 解析器正在对原始 URI 进行转义......这是这三个字符的解决方法:

<configSections> 
    <section name="uri" type="System.Configuration.UriSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> 
</configSections> 
<uri> 
    <schemeSettings> 
        <add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes"/> 
        <add name="https" genericUriParserOptions="DontUnescapePathDotsAndSlashes"/> 
    </schemeSettings> 
</uri>

(也公然从文章中窃取。)

于 2013-10-25T15:47:05.640 回答