2010-09-30 119 views
5

WCF將匹配這樣的:WCF/Rest/UriTemplate可變長度查詢字符串參數列表?

http://localhost:8888/test/blahFirst/blahSecond/sdfsdf,wwewe

這樣:

[OperationContract] 
[WebGet(UriTemplate = "test/{first}/{second}/{val1},{val2}")] 
string GetVal(string first, string second, string val1, string val2); 

有沒有一種方法,使VA11,VAL2是參數的可變長度的名單?所以它可能是val1,....,valN?並最終得到一個服務方法,如:

string GetVal(string first, string second, List<string> params); 

或沿着這些線?

回答

6

只需獲取一個簡單的字符串,然後使用拆分方法將其轉換爲方法中的數組(或列表)即可。

你的接口應該是這個樣子:

[OperationContract] 
[WebGet(UriTemplate = "test/{first}/{second}/{val1}")] 
string GetVal(string first, string second, string val1); 

的實現:

public string GetVal(string first, string second, string paramArray) 
    { 
     string[] parameters = paramArray.Split(','); 

     foreach (string parameter in parameters) 
     { 
      Console.WriteLine(parameter); 
     } 

     return "Hello"; 
    } 

並在瀏覽器調用它是這樣的:

http://localhost:8731/MyServer/test/first/second/1,2,3 

Take a look at the MSDN forum for a detailed answer

+0

Basicall我發現我的問題的答案是「不,你不能直接這樣做。」但是,是的,你的方法將起作用。無論如何,我會給它一張支票。 – MonkeyWrench 2011-01-14 16:43:41