I have created a WCF 3.5 application with a method named TestMe
as defined below:
[OperationContract]
[WebInvoke(UriTemplate = "/Login", Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
MyDictionary<string, string> TestMe(string param1, string param2);
MyDictionary
is created using this link: https://stackoverflow.com/a/7590189/546033
Everything here works fine. But the problem is when returning the data from the implemented method below:
MyDictionary<string, string> success = new MyDictionary<string, string>();
success["desc"] = "Test";
return success;
it returns following json:
{"TestMeResult":{"desc":"Test"}}
while what i need is:
{"success":{"desc":"Test"}}
where success
is the object name. What can be the workaround for this?
You can use MessageParameter attribute.
Controls the name of the request and response parameter names.
[OperationContract]
[WebInvoke(UriTemplate = "/Login", Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
[return: MessageParameter(Name = "success")]
MyDictionary<string, string> TestMe(string param1, string param2);
Just remove BodyStyle = WebMessageBodyStyle.Wrapped, it defaults to WebMessageBodyStyle.Bare, but have to explicitly declare it yourself.
EDIT:
Since you are dealing with JSON is not going to help becuase it works for XML style. So steps are:
You may also check this link to find out whats going on internally:
http://msdn.microsoft.com/en-us/library/bb412170.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With