WCF Rest服务接收对象为null

编程入门 行业动态 更新时间:2024-10-28 18:31:40
本文介绍了WCF Rest服务接收对象为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有WCF rest服务,希望将对象作为输入.

I have WCF rest service which is expecting object as input.

[WebInvoke(Method = "POST", UriTemplate = "SaveItem", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] [OperationContract] public string SaveItem(Item item) { string retValue; using (var business = new ItemBusiness()) { retValue = business.SaveItem(item).ToString(); } return retValue; }

Item类如下所示.

Item class looks like below.

[DataContract] public class Item { [DataMember] public string UserId { get; set; } [DataMember] public string Name { get; set; } [DataMember] public string Description { get; set; } [DataMember] public string Cost { get; set; } //[DataMember] //public string AvailableQunatity { get; set; } [DataMember] public string IsNegotiable { get; set; } [DataMember] public string LocationLat { get; set; } [DataMember] public string LocationLong { get; set; } [DataMember] public string Condition { get; set; } [DataMember] public string DeliveryType { get; set; } [DataMember] public string PostalCode { get; set; } [DataMember] public string Category { get; set; } }

我正在从android应用程序传递json.

I'm passing json from android application.

{"UserId":"1","Name":"1","Description":"1","Cost":"1","IsNegotiable":"1","LocationLat":"1," LocationLong:" 1," Condition:" 1," DeliveryType:" 1," PostalCode:" 1," Category:" 13}

{"UserId":"1","Name":"1","Description":"1","Cost":"1","IsNegotiable":"1","LocationLat":"1","LocationLong":"1","Condition":"1","DeliveryType":"1","PostalCode":"1","Category":"13"}

但是WCF方法将Item设为null不确定原因.

but the WCF method is getting Item as null not sure why.

任何帮助将不胜感激.

谢谢.

编辑

终于成功了...将 WebMessageBodyStyle.Wrapped 更改为 WebMessageBodyStyle.Bare

finally got it work...changed WebMessageBodyStyle.Wrapped to WebMessageBodyStyle.Bare

推荐答案

感谢分享您的解决方案.我需要WebMessageBodyStyle.Bare位,但是我的另一个问题是,按照其他网站上的建议,我的JSON被包装在父节点"中,例如:

Thanks for sharing your solution. I needed the WebMessageBodyStyle.Bare bit but my other issue was that my JSON was wrapped in a parent "node" per suggestions on other sites like this:

Not Working --> {"MethodParamName":[{"prop1":"value1","prop2":"value2"}]}

但是我的JSON始终为null.但是,一旦我删除了JSON的父节点",我的WCF终于可以读取该对象:

But my JSON was always null. However once I dropped the "parent node" of my JSON my WCF finally could read the object:

Working --> {"prop1":"value1","prop2":"value2"}

更多推荐

WCF Rest服务接收对象为null

本文发布于:2023-10-11 04:47:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1480729.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   WCF   Rest   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!