想象一下,我想使用WCF调用外部(意味着我无法控制合同)REST服务。 我有以下的合同WCF UriTemplate UrlEncode
[ServiceContract]
public interface ISomeRestApi
{
[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "blablabla/{parameter1}/{parameter2}")]
void PutSomething(string parameter1, string parameter2);
}
再说说我的参数之一是正斜杠(/)
public class Test{
[Fact]
public void TestPutSomething()
{
ISomeRestApi api = CreateApi();
//this results in the url: http://server/blablabla///someotherparam
api.PutSomething("/", "someotherparam");
//this also results in the url: http://server/blablabla///someotherparam
api.PutSomething(HttpUtility.UrlEncode("/"), "someotherparam");
//but i want: http://server/blablabla/%2F/someotherparam
}
}
如何强制WCF来urlencode我UriTemplate路径参数?
可能的重复[如何将斜杠和其他'url敏感'字符传递给WCF REST服务?](http://stackoverflow.com/questions/7176726/how-can-i-pass-slash-and -other-url-sensitive-characters-to-a-wcf-rest-service) – 2016-02-04 18:48:48
由于我无法控制被调用的外部服务,因此链接问题中的答案并非此问题的有效答案,因此更改服务合同中的uritemplate是不可能的。我修改了我的问题以反映这个约束。 – Stif 2016-03-12 16:53:16