Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass non-string to WCF RESTful service using UriTemplate?

Can I do the following?

[OperationContract] [WebGet(UriTemplate = "/foo/{id}")] string GetFoo(int id); 

I'd like my service to function as both RESTful service and RPC-style SOAP service. If possible I'd like to retain int as int, and not do parsing by hand.

like image 902
Eugene Yokota Avatar asked Feb 13 '09 13:02

Eugene Yokota


People also ask

What is UriTemplate in WCF?

UriTemplate is a class that encapsulates a URI template.

What is WebInvoke method in WCF?

The WebInvoke attribute exposes services using other HTTP verbs such as POST, PUT, and DELETE. The default is to use POST, but it can be changed by setting the Method property of the attribute. These operations are meant to modify resources; therefore, the WebInvoke attribute is used to make modifications to resources.


2 Answers

As dthrasher mentioned, move id to the query part of the URI. This worked for me:

[OperationContract] [WebGet(UriTemplate = "/foo?id={id}")] string GetFoo(int id); 

See "URI scheme" on wikipedia for more info about the different parts of a URI: http://en.wikipedia.org/wiki/URI_scheme

like image 169
Cameron Taggart Avatar answered Oct 12 '22 12:10

Cameron Taggart


If I remember correctly, UriTemplate variables in the path always resolve to strings when using WebGet or WebInvoke. You can only bind UriTemplate variables to int, long, etc. when they are in the query portion of the UriTemplate.

like image 24
dthrasher Avatar answered Oct 12 '22 11:10

dthrasher