WCF REST服务 - 带反斜杠的变量

时间:2013-10-25 15:36:16

标签: c# wcf rest url

我有以下合同:

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

设备的ID可以包含反斜杠“\”和“&”因为它代表了设备的路径。

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

有什么方法可以阻止这种情况吗?

1 个答案:

答案 0 :(得分:2)

看一下this article here,了解如何处理传递给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>

(也从文章中公然偷走了。)