繁体   English   中英

WCF REST服务-自托管Windows服务-如何在URL中使用%

[英]WCF REST Service - Self Host Windows Service - How to use % in URL

我有一个REST WCF服务,该服务具有一种将参数作为字符串获取的方法

当我在网址中使用%23时,出现错误消息:找不到端点! 例如:

-- Id #9999
http://localhost:8000/MyService/GetData/Id/%239999 (%23 means # symbol encoded)

如果我不带%符号就可以正常工作

http://localhost:8000/MyService/GetData/Id/10

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetData/id/{id}")]
    string GetData(string value);

}

我在Windows服务上托管服务:

        ServiceHost host = new ServiceHost(typeof(Service1), "http://localhost:8000");

        WebHttpBinding binding = new WebHttpBinding();

        ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IService1), binding, "MyService");
        WebHttpBehavior httpBehavior = new WebHttpBehavior();         
        endpoint.Behaviors.Add(httpBehavior);

        host.Open();

我发现了这篇文章: http : //www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx,但是由于我不在IIS上托管WCF而在Windows Service上托管了WCF,所以它不起作用

由于此线程,我找到了解决方案:
如何将斜杠和其他“ URL敏感”字符传递给WCF REST服务?

解:

<configuration>
  <system.net>
    <settings>
      <httpListener unescapeRequestUrl="false"/>
    </settings>
  </system.net>
</configuration>

在网址中使用一些特殊字符不是最好的做法。 请考虑不要使用它们。

看一下MSDN-UriTemplate

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM