我创建了一个 WCF 3.5 应用程序,其方法名为 TestMe
,定义如下:
[OperationContract]
[WebInvoke(UriTemplate = "/Login", Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
MyDictionary<string, string> TestMe(string param1, string param2);
MyDictionary
是使用此链接创建的:https://stackoverflow.com/a/7590189/546033
这里一切正常。但问题是从下面实现的方法返回数据时:
MyDictionary<string, string> success = new MyDictionary<string, string>();
success["desc"] = "Test";
return success;
它返回以下 json:
{"TestMeResult":{"desc":"Test"}}
而我需要的是:
{"success":{"desc":"Test"}}
其中 success
是对象名称。有什么解决方法?
最佳答案
您可以使用 MessageParameter属性。
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);
关于c# - 更改从 wcf 服务方法返回的 json 对象包装器名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12744974/