【问题标题】:WCF Rest service how to Access post JSON data with out parametersWCF Rest服务如何访问没有参数的post JSON数据
【发布时间】:2012-12-31 01:55:46
【问题描述】:

我创建了非常适合 get 的 restful wcf 服务。现在我正在构建一些发布方法。客户端以 JSON 格式将数据发布到此服务。服务方法不会有任何参数,所以我需要从发出的请求中读取 jsondata。我无法弄清楚我应该如何检索从请求中生成的数据。

 [OperationContract]
 [WebInvoke(Method = "POST", UriTemplate = "/SaveEmployee", RequestFormat =    WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]

public bool SaveEmployee()
        {
            //Capture Employee Object Data here and perform save
            return true;
        }

        [DataContract]
        public class Employee
        {
            [DataMember]
            public int Id
            {
                get;
                set;
            }

            [DataMember]
            public string Name
            {
                get;
                set;
            }
        }

【问题讨论】:

    标签: .net json wcf wcf-rest


    【解决方案1】:

    如果您使用数据协定类型设置您的方法,它将填充请求正文并为您反序列化。

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "/SaveEmployee", RequestFormat =            WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    public bool SaveEmployee(Employee sentEmployee)
        {
            //Capture Employee Object Data here and perform save
            return true;
        }
    

    如果您不能这样做,则必须在 URL 参数中传递 json 数据并手动反序列化内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多