tags:

views:

1398

answers:

2

I am building a REST service on WCF, and one of the methods I am writing is GetProfile, which will return the profile for a given username. The username will include the user's domain, and so will have the following format: "DOMAIN\username".

I therefore have a service called Profiles.svc, which has the following endpoint set up:

[OperationContract]
[WebGet(UriTemplate = "/{username}", ResponseFormat = WebMessageFormat.Xml)]
IRestResponse GetProfile(String username);

However, when I attempt to visit the service at http://server/profiles.svc/DOMAIN%5cusername (%5c is the urlencoded form of a backslash) I get the following error:

Server error in '/' Application
HTTP Error 400 - Bad Request

This occurs even when there is no code actually defined in my implementation of GetProfile, so I believe the error is being thrown at the point WCF attempts to bind the URI to a method.

Are there some settings I need to add to my web service configuration in order to allow backslashes to be added to URLs in a REST WCF service? Or are backslashes simply not allowed?

+3  A: 

I suspect that they are simply not allowed in that part of the url. They are fine in the query string as long as they are encoded. As a general rule i wouldn't put anything in the path part of the url that is invalid in filename (i.e. ?).

Perhaps you could use a different character to deliminate th domain from the username.

You could also seperate the username into domainname and username and use the normal path deliminator '/'. I'm not familar with the this enough to know if this is how you do it for sure, but this would be my best guess.

[OperationContract]
[WebGet(UriTemplate = "/{domainName}/{username}", ResponseFormat = WebMessageFormat.Xml)]
IRestResponse GetProfile(String domainName, String username);
Greg Dean
This seems to be the right way to do it (/{domainName}/{userName}) as domain and name have a hierarchial relationship that is expressed well with REST.
James Bender
+1  A: 

Thanks for the response. The clarification that %5c might be allowed in the querystring, but not in the path is useful. And yes, I was coming to the conclusion that separating the domain name and username into separate parts of the URI path is the way forward.

Amit
If my answer helped you, perhaps you could accept it.
Greg Dean
@amitkoth: also, rather than post an answer to respond to somebody, please use the comment section or even edit your original question.
Sailing Judo