WCF中URITemplate中的可选查询字符串参数? [英] Optional query string parameters in URITemplate in WCF?
问题描述
我正在WCF 4.0中开发一些RESTful服务.我有如下方法:
I'm developing some RESTful services in WCF 4.0. I've got a method as below:
[OperationContract]
[WebGet(UriTemplate = "Test?format=XML&records={records}", ResponseFormat=WebMessageFormat.Xml)]
public string TestXml(string records)
{
return "Hello XML";
}
因此,如果我将浏览器导航到 http://localhost:8000/Service/Test?format = XML& records = 10 ,那么一切都按已运行的方式进行.
So if i navigate my browser to http://localhost:8000/Service/Test?format=XML&records=10, then everything works as exepcted.
但是,我希望能够导航到 http://localhost:8000/Service/测试?format = XML 并忽略URL的& records = 10"部分.但是现在,由于URI与预期的URI模板不匹配,我收到了服务错误.
HOWEVER, i want to be able to navigate to http://localhost:8000/Service/Test?format=XML and leave off the "&records=10" portion of the URL. But now, I get a service error since the URI doesn't match the expected URI template.
那么如何为某些查询字符串参数实现默认值?我想将记录"默认设置为10(例如,如果该部分不包含在查询字符串中).
So how do I implement defaults for some of my query string parameters? I want to default the "records" to 10 for instance if that part is left off the query string.
推荐答案
注意:该问题已过时,请参阅其他答案.
这似乎不受支持.
但是,Microsoft已经意识到此问题,并且有一种解决方法:
However, Microsoft has been made aware of this issue and there is a work-around:
您可以通过以下方式获得所需的效果 从中省略查询字符串 WebGet上的UriTemplate或 WebInvoke属性,并使用 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 from within your handlers to inspect, set defaults, etc. on the query parameters.
https://connect.microsoft.com/VisualStudio/feedback/details/451296/
这篇关于WCF中URITemplate中的可选查询字符串参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!