[英]WCF REST Service - Self Host Windows Service - How to use % in URL
I have a REST WCF service that has a method that gets a parameter as a string 我有一个REST WCF服务,该服务具有一种将参数作为字符串获取的方法
When I use the %23 in the Url I got an error message: Endpoint not found! 当我在网址中使用%23时,出现错误消息:找不到端点! eg:
例如:
-- Id #9999
http://localhost:8000/MyService/GetData/Id/%239999 (%23 means # symbol encoded)
If I use without % symbol it works fine 如果我不带%符号就可以正常工作
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);
}
I Host the Service on Windows Service: 我在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();
I've found this post: http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx but it doesn't work since I'm not hosting WCF on IIS I'm hosting it on Windows Service instead 我发现了这篇文章: http : //www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx,但是由于我不在IIS上托管WCF而在Windows Service上托管了WCF,所以它不起作用
I found the solution thanks to this thread: 由于此线程,我找到了解决方案:
How can I pass slash and other 'url sensitive' characters to a WCF REST service? 如何将斜杠和其他“ URL敏感”字符传递给WCF REST服务?
Solution: 解:
<configuration>
<system.net>
<settings>
<httpListener unescapeRequestUrl="false"/>
</settings>
</system.net>
</configuration>
its not a best pratice to use some special chars in urls. 在网址中使用一些特殊字符不是最好的做法。 please so consider not using them.
请考虑不要使用它们。
take a look at MSDN - UriTemplate 看一下MSDN-UriTemplate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.