1

我正在使用 WCF 创建一个 Rest-JSON API。

问题是我的结果总是以{"GetResult": HERE_MY_RESULT }(注意“GetResult”)开头

例如:

public string GetString()
{
    return "Hello World!";
}

返回{"GetResult": "Hello World!"}

这是我用于服务的相关代码:服务:

[ServiceContract]
public interface IPlaceService
{
    [OperationContract]
    [WebInvoke(Method = "GET",
                ResponseFormat = WebMessageFormat.Json,
                BodyStyle = WebMessageBodyStyle.Wrapped,
                UriTemplate = "places")]
    PlaceModel Get();
}

public class PlaceService : IPlaceService
{
    public PlaceModel Get()
    {
        return new PlaceModel
        {
            Count = 123,
            Title = "Title",
            Description = "Desc",
        };
    }
}

合同:

[DataContract]
public class PlaceModel
{
    [DataMember(Name="count")]
    public int Count { get; set; }

    [DataMember(Name = "title")]
    public string Title { get; set; }

    [DataMember(Name = "description")]
    public string Description { get; set; }
}

结果:

{"GetResult":{"count":123,"description":"Desc","title":"Title"}}

有人知道如何"GetResult"从我的 JSON 结果中删除它吗?

提前致谢。

4

1 回答 1

0

好吧,我在这篇文章中找到了解决方案。

只需更改BodyStyleto[OperationContract]

BodyStyle = WebMessageBodyStyle.Bare,

修复它,现在我收到:

{"count":123,"data":[],"description":"Desc","title":"Title"}

于 2013-01-09T19:28:30.627 回答