使用查询字符串参数消除UriTemplate匹配的歧义 [英] Using query string parameters to disambiguate a UriTemplate match

查看:179
本文介绍了使用查询字符串参数消除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);

这篇关于使用查询字符串参数消除UriTemplate匹配的歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆