简体   繁体   English

如何在WCF REST服务中使用可选参数?

[英]How to have optional parameters in WCF REST service?

What I am looking for is a way I can pass anything to a specific url so I can parse it myself. 我正在寻找的是一种我可以将任何东西传递给特定网址的方式,所以我可以自己解析它。 Something like this : 像这样的东西:

    [OperationContract]
    [WebGet(UriTemplate = "/whatever/blabla/{query}", RequestFormat = WebMessageFormat.Xml)]
    string AddRouteForUser(string query);

Afterwards I can parse query myself to have the values I need. 之后我可以自己解析查询以获得我需要的值。 Is it possible ? 可能吗 ? Is there a better way ? 有没有更好的办法 ?

Thanks 谢谢

[Edit title] [编辑标题]

One option would be to use query string params since by definition, they're sort of an optional property bag of options tagged onto a query. 一种选择是使用查询字符串参数,因为根据定义,它们是一种标记在查询上的可选属性包。 That leaves the path to being an immutable identifier for your rest resource. 这使得路径成为您的其余资源的不可变标识符。

/whatever/blabla?some=data&whichis=optional

It's hard to say though without sepcific knowledge of the data and the rest resources 虽然没有对数据和其他资源的熟悉知识,但很难说

EDIT: 编辑:

QueryStrings can be optional. QueryStrings可以是可选的。
See comment from Anand @ https://connect.microsoft.com/VisualStudio/feedback/details/451296/rest-wcf-uritemplate-optional-querystring-parameters 请参阅Anand的评论@ https://connect.microsoft.com/VisualStudio/feedback/details/451296/rest-wcf-uritemplate-optional-querystring-parameters

"You can get the desired effect by omitting the Query string from the UriTemplate on your WebGet or WebInvoke attribute, and using WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters" “通过在WebGet或WebInvoke属性上省略UriTemplate中的Query字符串,并使用WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters,可以获得所需的效果”

At the end I am going to use OperationContext to get the complete url and parse it myself. 最后,我将使用OperationContext获取完整的URL并自己解析它。 I just put in WebGet this /whatever/{query} so I know what I want to treat. 我只是把这个/不管/ {查询}放入WebGet所以我知道我想要对待什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM