WCF和可选参数

| 我刚刚开始将WCF与REST和UriTemplates结合使用。现在可以使用可选参数了吗? 如果不是,那么您会建议我为一个具有三个始终在url中使用且其他参数是可选(可变数量)的参数的系统做什么? 例:
https://example.com/?id=ID&type=GameID&language=LanguageCode&mode=free 
ID,类型,语言始终存在 模式是可选的     
已邀请:
我刚刚使用WCF 4对其进行了测试,并且工作正常。如果我在查询字符串中不使用mode,我将得到null作为参数值:
[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 ?? \"\";
    }
}
对我来说,所有查询字符串参数都是可选的。如果查询字符串中不存在参数,则参数类型默认值为=>
null
string
),0(05ѭ等)。MS还声明应实现此值。 无论如何,您始终可以用
id
type
language
定义
UriTemplate
,并通过
WebOperationContext
访问方法内部的可选参数:
var mode = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters[\"mode\"];
    
我尝试过在Restful Web服务中使用可选参数, 如果我们没有在参数值中传递任何内容,则它保持为空。之后,我们可以检查 函数中的null或空。如果为null,则不要使用它,否则可以使用它。 假设我有以下代码
[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebGet(UriTemplate = \"GetSample?part1={part1}&part2={part2}\")]
    string GetSample(string part1, string part2);
}
在这里,part1是必需的,而part2是可选的。 现在函数看起来像
public class Service : IService
{
    public string GetSample(string part1, string part2)
    {
        if (!string.IsNullOrEmpty(part2))
        {
            return \"Hello friends...\" + part1 + \"-\" + part2;
        }
        return \"Hello friends...\" + part1;
    }
}
您也可以根据需要进行转换。     
您必须在网址中使用\“?\”,然后使用\“ / \”。 例:
[WebGet(UriTemplate = \"GetSample/?OptionalParamter={value}\")]
    string GetSample(string part1);
    

要回复问题请先登录注册