使用查询字符串参数消除UriTemplate匹配的歧义
我正在使用WCF 4.0创建REST风格的Web服务.我想做的是根据UriTemplate
中的查询字符串参数调用不同的服务方法.
I am using WCF 4.0 to create a REST-ful web service. What I would like to do is have different service methods called based on query string parameters in the UriTemplate
.
例如,我有一个API,该API允许用户使用其驾驶执照或社会保险号作为密钥来检索有关某个人的信息.在我的ServiceContract
/接口中,我将定义两个方法:
For example, I have an API that allows users to retrieve information about a person using either by their driver's license or their social security number as a key. In my ServiceContract
/ interface I would define two methods:
[OperationContract]
[WebGet(UriTemplate = "people?driversLicense={driversLicense}")]
string GetPersonByLicense(string driversLicense);
[OperationContract]
[WebGet(UriTemplate = "people?ssn={ssn}")]
string GetPersonBySSN(string ssn);
但是,当我使用这两种方法调用服务时,都会出现以下异常:
However, when I call my service with both methods I get the following exception:
UriTemplateTable不支持具有以下内容的多个模板 与模板"people?ssn = {ssn}"等效的路径,但具有不同的路径 查询字符串,其中不能全部通过以下方式消除查询字符串的歧义 文字值.有关更多信息,请参见UriTemplateTable的文档. 细节.
UriTemplateTable does not support multiple templates that have equivalent path as template 'people?ssn={ssn}' but have different query strings, where the query strings cannot all be disambiguated via literal values. See the documentation for UriTemplateTable for more detail.
使用UriTemplates
不能做到这一点吗?这似乎是一种常见情况.
Is there not some way to do this with UriTemplates
? It seems like a common scenario.
非常感谢!
According to This post, it is not possible, you would have to do something like:
[OperationContract]
[WebGet(UriTemplate = "people/driversLicense/{driversLicense}")]
string GetPersonByLicense(string driversLicense);
[OperationContract]
[WebGet(UriTemplate = "people/ssn/{ssn}")]
string GetPersonBySSN(string ssn);