简体   繁体   中英

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

When I use the %23 in the Url I got an error message: Endpoint not found! 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:

        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

I found the solution thanks to this thread:
How can I pass slash and other 'url sensitive' characters to a WCF REST service?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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