使用查询字符串参数消除UriTemplate匹配的歧义

使用查询字符串参数消除UriTemplate匹配的歧义

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

使用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匹配的歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 05:16