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

.NetWcfWeb ServicesRest

.Net Problem Overview


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.

.Net Solutions


Solution 1 - .Net

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

Solution 2 - .Net

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.

Solution 3 - .Net

As others mentioned, you must use query strings in order to pass non-string parameters. The following article details how the parsing is done.

WCF Extensibility – QueryStringConverter

> Coming back to “proper” > WCF extensibility, this week’s post is about the QueryStringConverter. > This is actually a simple topic to be covered, as its purpose is quite > specific (unlike other extensibility points seen before, which could > be used for a wide variety of cases) – within WCF the > QueryStringConverter is only used on endpoints which have the > WebHttpBehavior applied to them. And even in those, only on operations > which have parameters passed via the query strings (either operations > with parameters marked with [WebGet] or a [WebInvoke] operation with a > UriTemplate that explicitly binds some parameters to the query > string). A QueryStringConverter is the piece which can convert between > operation parameters and their representation in a query string. > ...

> The default QueryStringConverter used by the WebHttpBehavior supports > natively several types, including all simple numeric types (Byte, > SByte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, > Decimal), Boolean, Char, Object, String, DateTime, DateTimeOffset, > TimeSpan, Guid, Uri, and arrays of Byte (essentially, all the types > which the DataContractSerializer considers to be “primitives”, with > the exception of XmlQualifiedName). Enumeration types are also > supported by default (the string representation of the enum values are > used). Finally, there is also another set of types which are supported > by the default QueryStringConverter – any one which declares a > [TypeConverter] attribute with a type converter which can convert the > type to and from strings (more on that below).

Solution 4 - .Net

Unfortunately you must do the parsing yourself if you want to use the UriTemplate.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEugene YokotaView Question on Stackoverflow
Solution 1 - .NetCameron TaggartView Answer on Stackoverflow
Solution 2 - .NetdthrasherView Answer on Stackoverflow
Solution 3 - .NetOhad SchneiderView Answer on Stackoverflow
Solution 4 - .NetAndrew HareView Answer on Stackoverflow