开发者

when to use UriTemplate in REST WCF service

开发者 https://www.devze.com 2023-01-25 08:53 出处:网络
i have simple interface that i want to test it out but i have\'nt understood when to use URITemplate:

i have simple interface that i want to test it out but i have'nt understood when to use URITemplate:

how would i access XMLData in this case...?

[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;
        }


UriTemplate is some kind alike of masking your method. Example:

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

you may now call the method this way:

http://localhost/RestServiceImpl/myMethod/inputIdstring  

instead of...

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

I hope this helps..


By default, if you don't specify a UriTemplate, WCF will supply one for you that uses a query string format, such as this:

XMLData?id={id}

However, you might want a RESTful URI, instead, like this:

xmldata/{id}  

For those cases, you add a UriTemplate. If you don't need anything but the default semantics, feel free to leave it off.

0

精彩评论

暂无评论...
验证码 换一张
取 消