Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use an object in UriTemplate?

Tags:

rest

c#

wcf

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

like image 774
Xaisoft Avatar asked Sep 17 '10 22:09

Xaisoft


1 Answers

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.

like image 186
Steve Michelotti Avatar answered Oct 23 '22 01:10

Steve Michelotti