.net WCF 和可选参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5781342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WCF and optional parameters
提问by chobo
I just started using WCF with REST and UriTemplates. Is it now possible to use optional parameters?
我刚刚开始将 WCF 与 REST 和 UriTemplates 结合使用。现在是否可以使用可选参数?
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)?
如果没有,你们会建议我为一个系统做什么,该系统具有三个始终在 url 中使用的参数,而其他参数是可选的(数量不等)?
Example:
例子:
https://example.com/?id=ID&type=GameID&language=LanguageCode&mode=free
- id, type, language are always present
- mode is optional
- id、type、language 始终存在
- 模式是可选的
回答by Ladislav Mrnka
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:
我刚刚用 WCF 4 对其进行了测试,它没有任何问题。如果我不在查询字符串中使用 mode 我将得到 null 作为参数的值:
[ServiceContract]
public interface IService
{
[OperationContract]
[WebGet(UriTemplate = "GetData?data={value}&mode={mode}")]
string GetData(string value, string mode);
}
Method implementation:
方法实现:
public class Service : IService
{
public string GetData(string value, string mode)
{
return "Hello World " + value + " " + mode ?? "";
}
}
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 => nullfor string, 0 for int, etc. MS also statesthat this should be implemented.
对我来说,所有查询字符串参数都是可选的。如果查询字符串中不存在参数,则其类型将具有默认值 => nullfor string、 0 forint等。MS 还指出应该实现这一点。
Anyway you can always define UriTemplatewith id, typeand languageand access optional parameters inside method by WebOperationContext:
无论如何,您始终可以UriTemplate通过以下方式定义with id, typeandlanguage并访问方法内的可选参数WebOperationContext:
var mode = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["mode"];
回答by Satyen Pandya
I have tried with optional parameters in restful web service, If we do not pass anything in parameter value it remains null. After that we can check for the null or empty in function. If it is null then don't use it, else you can use it. Let say I have below code
我尝试过在restful web 服务中使用可选参数,如果我们没有在参数值中传递任何内容,它仍然为空。之后我们可以检查函数中的空值或空值。如果它为空,则不要使用它,否则您可以使用它。假设我有以下代码
[ServiceContract]
public interface IService
{
[OperationContract]
[WebGet(UriTemplate = "GetSample?part1={part1}&part2={part2}")]
string GetSample(string part1, string part2);
}
Here part1 is compulsory and part2 is optional. Now the function would look like
这里第 1 部分是强制性的,第 2 部分是可选的。现在函数看起来像
public class Service : IService
{
public string GetSample(string part1, string part2)
{
if (!string.IsNullOrEmpty(part2))
{
return "Hello friends..." + part1 + "-" + part2;
}
return "Hello friends..." + part1;
}
}
You can also make the conversion based on your requirements.
您也可以根据您的要求进行转换。
回答by Shakeer Hussain
You have to use "?" followed by "/" in your Url.
你必须使用“?” 后跟“/”在您的网址中。
example:
例子:
[WebGet(UriTemplate = "GetSample/?OptionalParamter={value}")]
string GetSample(string part1);

