传递给WCF服务的可选查询字符串参数 [英] Optional query string parameter passing to WCF service
问题描述
我想知道如何使用:
string limit = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["Limit"];
在我的wcf中采用这种方法:
in my wcf in this method:
CityNewsList GetNewsByCity(string DeviceType,string id,string limit);
此处"devicetype"和"id"是默认参数,而我想要的是"limit",因为这是可选参数,表示用户可以选择传递此参数,他可以通过也可以不通过.
here 'devicetype' and 'id' are default parameter and what i want is 'limit' as optional parameter means user have a choice to pass this parameter he can pass or can not pass this.
想要将限制用作:
if (limit == some value)
{
//do this.
}
if (limit == null)
{
// do this.
}
我浏览了很多链接,但是我没有在wcf中使用该链接.
i go through many links but i didn't get that how to use this in my wcf.
或者是否有人可以告诉我如何在WCF服务中使参数成为可选参数.
or if someone can tell me how to make a parameter optional in the WCF Service.
推荐答案
实际上,您是在使用WCF创建REST服务.我已经读过您要创建的可能重复项的问题的答案中的含义:
So actually you're using WCF to create a REST service. I've read what you mean in the answer to the question you're creating a possible duplicate of: How to have optional parameters in WCF REST service?
您可以通过从WebGet或WebInvoke属性上的UriTemplate中省略查询字符串,并使用WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters
来获得所需的效果.
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
.
所以,归结为:
更改方法的签名以省略参数:
Change your method's signature to omit the parameter:
CityNewsList GetNewsByCity(string DeviceType,string id /*,string limit*/);
更改属性,以使查询字符串上不需要该参数:
Change the attributes so that the parameter is not expected on the query string:
[OperationContract]
[WebGet(UriTemplate = "/whatever/{DeviceType}/{id}", RequestFormat = WebMessageFormat.Xml)]
代替
[OperationContract]
[WebGet(UriTemplate = "/whatever/{DeviceType}/{id}/{limit}", RequestFormat = WebMessageFormat.Xml)]
最后,您会得到类似的东西:
In the end you'd have something like:
[OperationContract]
[WebGet(UriTemplate = "/whatever/{DeviceType}/{id}", RequestFormat = WebMessageFormat.Xml)]
CityNewsList GetNewsByCity(string DeviceType,string id);
实现的第一件事是:
string limit = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["Limit"];
但是:我还没有尝试过,但这是我从您对问题的评论中引用的内容所得到的理解.
However: I have not tried that, but that's what I understand from what you've quoted in the comments to your question.
这篇关于传递给WCF服务的可选查询字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!