想象一下,我想使用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