我正在尝试在接受 JSON 列表的 dotnet(VS 2012)中创建一个安静的 Web 服务。有这个非常有用的帖子。
该示例是正确的,但我需要对 DataContract 进行排序。这是我的问题:
提琴手帖子:
{ "Markers": [
{ "position": "128.3657142857143", "markerPosition": "7" },
{ "position": "235.1944023323615", "markerPosition": "19" },
{ "position": "42.5978231292517", "markerPosition": "-3" }
]};
在 VS2012 调试器中,我看到了等价物:
{ "Markers": [
{ "position": "0", "markerPosition": "0" },
{ "position": "0", "markerPosition": "0" },
{ "position": "0", "markerPosition": "0" }
]};
这是代码(直接来自链接的示例):
public class Marker
{
decimal position { get; set; }
int markerPosition { get; set; }
}
public string CreateMarkers(List<Marker> Markers)
{
return "Received " + Markers.Count + " markers.";
}
这是合同:
[OperationContract]
[WebInvoke(UriTemplate = "Markers", Method = "POST", ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
string CreateMarkers(List<Marker> Markers);
另一个同样精彩的帖子解释了需要设置数据合同以查看参数值:
我最初的问题是数据合同应该是什么。我开始:
[OperationContract]
[WebInvoke(UriTemplate = "Markers", Method = "POST", ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
string CreateMarkers(List<Marker> Markers);
之后消息结构通过,但内容为空。然后,根据建议,我尝试过:
[OperationContract]
[WebInvoke(UriTemplate = "Markers", Method = "POST", ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
string CreateMarkers(List<Marker> Markers);
之后消息结构和内容为空。
[OperationContract]
[WebInvoke(UriTemplate = "WrappedMarkers", Method = "POST", ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
string WrappedMarkers(MarkerRequest wrappedmarkers);
[DataContract]
public class MarkerRequest
{
[DataMember]
public List<Marker> Markers
{
get { return _markers; }
set { _markers = value; }
}
private List<Marker> _markers = new List<Marker>();
}
public string WrappedMarkers(MarkerRequest wrappedmarkers)
{
return wrappedmarkers.Markers.ToString();
}
这是收缩的操作,更改为 WrappedRequest
结构是正确的,但内容又是空的。
回到原点。我似乎能够看到消息的结构。如何将内容映射到结构?