change the object wrapper name json returned by the wcf service method

advertisements

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);