在 REST WCF URL 中使用反斜杠 (%5c) [英] Using a backslash (%5c) in a REST WCF URL
问题描述
我正在 WCF 上构建 REST 服务,我正在编写的方法之一是 GetProfile,它将返回给定用户名的配置文件.用户名将包含用户的域,因此格式如下:DOMAIN\username".
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".
因此,我有一个名为 Profiles.svc 的服务,它设置了以下端点:
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);
但是,当我尝试访问 http://server/profiles.svc/DOMAIN 上的服务时%5cusername(%5c 是反斜杠的 urlencoded 形式)我收到以下错误:
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
即使在我的 GetProfile 实现中没有实际定义代码,也会发生这种情况,所以我相信在 WCF 尝试将 URI 绑定到方法时抛出错误.
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.
是否需要将一些设置添加到我的 Web 服务配置中,以便允许将反斜杠添加到 REST WCF 服务中的 URL?还是根本不允许使用反斜杠?
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?
推荐答案
我怀疑他们根本不允许出现在 url 的那部分.只要它们被编码,它们就可以在查询字符串中使用.作为一般规则,我不会在 url 的路径部分放置任何文件名无效的内容(即?).
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);
这篇关于在 REST WCF URL 中使用反斜杠 (%5c)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!