1

Here is a WCF method to send response in JSON format.

[OperationContract(Name = "Employee")]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedResponse, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "person/{name}")]
Person GetPersonData(string name);

Here is the format I'm getting:

{"EmployeeResult":{"Age":31,"Name":"testuser"}}

I need something like below:

{"Employee":{"Age":31,"Name":"testuser"}}

I tried to change

BodyStyle = WebMessageBodyStyle.Bare

but getting below Format:

{"Age":31,"Name":"testuser"}

Is there anyother thing I need to change ? I'm working on .net framework 4.5

Thanks

Ashok
  • 601
  • 1
  • 17
  • 32
  • Did you check this http://stackoverflow.com/questions/20206069/restful-web-service-body-format/20225936#20225936 ? – Rockstar Jul 28 '15 at 12:21

1 Answers1

3

You can use MessageParameterAttribute:

[OperationContract(Name = "Employee")]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedResponse, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "person/{name}")]
[return:MessageParameter(Name = "Employee")]
Person GetPersonData(string name);
Maku
  • 1,464
  • 11
  • 20
  • @Make perfect, JSON arrays are written inside square brackets. Do I need to change anything? when method return JSON arrays? – Ashok Jul 28 '15 at 12:53
  • As long BodyStyle is set to WrappedResponse, ultimately the result will be an object with single property (which can be an array then). It does not change anything in this case. – Maku Jul 28 '15 at 13:38