2010-11-17 94 views
1

我有簡單的界面,我想測試它,但我have'nt理解時使用URITemplate:時使用UriTemplate在REST WCF服務

我怎麼會訪問XMLDATA在這種情況下...?

[OperationContract] 
     [WebInvoke(Method = "GET", 
      ResponseFormat = WebMessageFormat.Xml, 
      BodyStyle = WebMessageBodyStyle.Wrapped)] 
     string XMLData(string id); 

public class RestServiceImpl : IRestServiceImpl 
    {  
     public string XMLData(string id) 
     { 
      return "my xml data:" + id; 
     } 

回答

1

UriTemplate是掩蓋你的方法的一種相似。例如:

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "myMethod/{id}")] 
string XMLData(string id); 

你現在可以調用該方法是這樣的:

http://localhost/RestServiceImpl/myMethod/inputIdstring 

的,而不是...

http://localhost/RestServiceImpl/XMLData?id=inputIdstring 

我希望這有助於..

0

默認情況下,如果不指定UriTemplate,WCF會爲您提供一個使用查詢字符串格式,比如這個:

XMLData?id={id} 

但是,您可能需要一個RESTful URI,而不是像這樣:

xmldata/{id} 

對於這些情況,您可以添加一個UriTemplate。如果您不需要除默認語義之外的任何內容,請隨時離開它。

+0

如果我使用用於POST數據的RESTful URI比我要怎麼做? – 2010-11-17 21:02:13

+0

@Randolpho請編輯您的答案,以便我可以收回我意外的投票。 – 2013-07-16 07:45:07

+0

呃...好的。全部編輯:) – Randolpho 2013-07-16 21:15:49