WCF
RESTful Service
UriTemplate
Non-String Data
Web Services

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

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In a WCF REST service, everything in the URI travels as text, but your service method does not have to receive it as a string. UriTemplate values can be converted into simple typed parameters such as int, bool, Guid, and DateTime as long as the incoming text can be parsed correctly. The real limit is not whether non-string types are possible, but whether the data belongs in the URI at all.

URI Parameters Are Text on the Wire

A request like /users/42 contains the characters 4 and 2 in the path. WCF reads those characters from the URI and then tries to bind them to the parameter type declared in your operation contract.

That means a method like this is valid:

csharp
1[ServiceContract]
2public interface IUserService
3{
4    [OperationContract]
5    [WebGet(UriTemplate = "/users/{id}")]
6    string GetUser(int id);
7}

If the client requests /users/42, WCF converts the path segment into an int and calls GetUser(42).

Simple Scalar Types Work Well

WCF can usually bind common scalar types cleanly from path or query parameters.

csharp
[OperationContract]
[WebGet(UriTemplate = "/orders?customerId={customerId}&includeClosed={includeClosed}")]
string GetOrders(int customerId, bool includeClosed);

Typical candidates for URI binding are:

  • numeric identifiers
  • boolean flags
  • 'Guid values'
  • short enums or status codes
  • stable date strings

Those values are compact, human-readable, and fit the semantics of a resource locator or simple filter.

Example with Guid and DateTime

More than strings and integers can work, but formatting must be predictable.

csharp
[OperationContract]
[WebGet(UriTemplate = "/reports/{reportId}?date={date}")]
string GetReport(Guid reportId, DateTime date);

This can succeed, but only if the client sends a date string WCF can parse reliably. In practice, dates in URIs should use a documented, culture-independent format so that server and client do not disagree about what the value means.

Know When the Request Body Is Better

Just because WCF can parse simple types from a UriTemplate does not mean you should force every input into the URL. Complex objects, lists, nested filters, and large payloads belong in the request body.

csharp
1[OperationContract]
2[WebInvoke(
3    Method = "POST",
4    UriTemplate = "/users",
5    RequestFormat = WebMessageFormat.Json,
6    ResponseFormat = WebMessageFormat.Json
7)]
8User CreateUser(UserCreateRequest request);
9
10public class UserCreateRequest
11{
12    public string Name { get; set; }
13    public int Age { get; set; }
14}

This is easier to validate, version, and document than trying to encode a full object graph into query-string parameters.

Conversion Is Not Validation

Successful binding only means the text could be parsed into the requested type. It does not mean the value is valid for your business rules.

For example:

  • 'int id = 0 may parse correctly but still be an invalid identifier'
  • a DateTime may parse but be outside the allowed range
  • an enum value may parse but not make sense in the current workflow

You still need normal service-level validation after WCF finishes parameter binding.

Common Pitfalls

The most common misunderstanding is thinking URIs carry real binary or typed values. They do not. The URI is text, and WCF performs type conversion after parsing that text.

Another issue is using UriTemplate for complex request data that should be modeled as JSON or XML in the body. Long query strings are difficult to validate, hard to evolve, and unpleasant for clients to construct.

Dates are another trap. If the client and server do not agree on a precise date format, binding may fail or, worse, succeed with the wrong interpretation. Keep date formats explicit and culture-independent.

Finally, do not confuse typed binding with input safety. A parsed Guid or int still needs authorization checks, range checks, and any other domain validation your endpoint requires.

Summary

  • 'UriTemplate values are textual in the URI, but WCF can bind them to simple non-string parameter types.'
  • Types such as int, bool, Guid, and DateTime are common candidates for URI binding.
  • Complex objects and large payloads should usually be sent in the request body instead.
  • Successful conversion is only parsing, not business validation.
  • Keep URI parameters short, stable, and easy for clients to format correctly.

Course illustration
Course illustration

All Rights Reserved.