I’m using WCF to create a Rest-JSON API.
The problem is that my results always starts with {"GetResult": HERE_MY_RESULT } (notice the “GetResult”)
For example:
public string GetString()
{
return "Hello World!";
}
returns {"GetResult": "Hello World!"}
Here is the relevant code that I’m using for my service:
Service:
[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",
};
}
}
Contract:
[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; }
}
And the result:
{"GetResult":{"count":123,"description":"Desc","title":"Title"}}
Does anybody know how to remove that "GetResult" from my JSON result?
Thanks in advance.
Well I found the solution inspired on this post.
Just changing the
BodyStyleof the[OperationContract]tofixes it, and now Im recieving:
{"count":123,"data":[],"description":"Desc","title":"Title"}