72

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.

Fabian Steeg
  • 44,988
  • 7
  • 85
  • 112
Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319

4 Answers4

81

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

Cameron Taggart
  • 5,771
  • 4
  • 45
  • 70
69

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.

dthrasher
  • 40,656
  • 34
  • 113
  • 139
  • I am using a jagged array, and it is showing this message. Is there any way to pass the jagged array to the params? I am doing it like this `string MyMethod(string[][] jaggedArray);` – Jamshaid K. Sep 06 '17 at 06:49
5

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).

Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198
3

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

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635