WCF和可选参数 [英] WCF and optional parameters
问题描述
我刚开始使用WCF与REST和UriTemplates。它是目前可以使用可选的参数?
I just started using WCF with REST and UriTemplates. Is it now possible to use optional parameters?
如果没有,你会怎么家伙建议我做一个系统,有是可选的(变化量)三个参数总是用在URL,和其他人?
If not, what would you guys recommend I do for a system that has three parameters that are always used in the url, and others that are optional (varying amount)?
例如:
https://example.com/?id=ID&type=GameID&language=LanguageCode&mode=free
- 标识,类型,语言始终是present
- 模式可选
推荐答案
我只是测试它与WCF 4和它的工作没有任何问题。如果我不使用查询字符串模式我会得到null作为参数值:
I just tested it with WCF 4 and it worked without any problems. If I don't use mode in query string I will get null as parameter's value:
[ServiceContract]
public interface IService
{
[OperationContract]
[WebGet(UriTemplate = "GetData?data={value}&mode={mode}")]
string GetData(string value, string mode);
}
方法实现:
public class Service : IService
{
public string GetData(string value, string mode)
{
return "Hello World " + value + " " + mode ?? "";
}
}
对于我来说,它看起来像所有的查询字符串参数都是可选的。如果一个参数不是present的查询字符串,将有其类型=> 空
默认值字符串
0 INT
等<一href="http://connect.microsoft.com/VisualStudio/feedback/details/451296/rest-wcf-uritemplate-optional-querystring-parameters">MS还规定,这应该得到执行。
For me it looks like all query string parameters are optional. If a parameter is not present in query string it will have default value for its type => null
for string
, 0 for int
, etc. MS also states that this should be implemented.
反正你总是可以定义 UriTemplate
与 ID
,键入
和语言
键,由内而外的方法获取可选参数 WebOperationContext
:
Anyway you can always define UriTemplate
with id
, type
and language
and access optional parameters inside method by WebOperationContext
:
var mode = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["mode"];
这篇关于WCF和可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!