Reputation: 3194
In Wcf Rest serivce how to return custom Json? Like: if i have a Json result for a model
{'name':'R2D2', 'location':'starship'}
I want to have a custom Json Result like
{'Status':'OK', 'data':{'name':'R2D2', 'location':'starship'}, 'Message':'',...
}
How can i achieve this function?
I did some how achieve it... Not sure if it is the best way.
public class JsonResult<T>
{
public string Status { get; set; }
public string Message { get; set; }
public T Data { get; set; }
}
in Service
[WebGet(ResponseFormat=WebMessageFormat.Json)]
JsonResult<Robot> TestJson();
It does Give me my desired Result. Is this the best way? or There are other methods Too? Thanks
Upvotes: 2
Views: 1193
Reputation: 1016
If you want to control your response format you can implement a custom IDispatchMessageFormatter (a custom WebMessageFormat).
There is a nice post in here: http://serena-yeoh.blogspot.co.il/2013/02/wcf-rest-custom-webmessageformat.html
Upvotes: 2