何时在REST WCF服务中使用UriTemplate

时间:2010-11-17 20:00:31

标签: wcf wcf-client

我有简单的界面,我想测试它,但我不知道何时使用URITemplate:

在这种情况下我将如何访问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;
        }

2 个答案:

答案 0 :(得分:2)

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  

我希望这有帮助..

答案 1 :(得分:0)

默认情况下,如果您未指定UriTemplate,WCF将为您提供一个使用查询字符串格式的格式,例如:

XMLData?id={id}

但是,您可能需要RESTful URI,如下所示:

xmldata/{id}  

对于这些情况,您添加UriTemplate。如果除了默认语义之外不需要任何其他内容,请随意将其关闭。