C# – use an object in UriTemplate

crestwcf

I tried the following code:

 [OperationContract]
 [WebInvoke(UriTemplate="/Users/Register/{user}")]
 void Register(User user);

But when I Try to run this, it tells me the UriTemplate must only contain strings. What if I need to pass in an object to my method as in this case, a User object to my Register method.

If I change the WebInvoke attribute to:

[WebInvoke(UriTemplate="/Users/Register/")]

The browswer displays the error Method not allow when I try to browse to http://localhost:8000/Users/Register for example

Best Answer

You are limited to strings in the UriTemplate. You could use multiple parameters to pass multiple strings but you cannot use a complex type. If you want to pass a complex type, then it cannot be anywhere in the URI but rather in the body of a POST/PUT request. The GET request does not take a message body. So your above code could be changed to this:

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate="/Users/Register")]
void Register(User user);

Where you're passing the User object in, not via the Uri, but as part of the POST request.