WCF和可选参数 [英] WCF and optional parameters

查看:287
本文介绍了WCF和可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆