Ros*_*ush 4 c# asp.net wcf json
我正在尝试查找有关如何通过ajax调用而不是使用默认包装器来修改从wcf服务以json格式返回给Web客户端的对象名称的信息。我没有运气就搜索了相关文章。似乎结果的默认包装器名称将是MethodNameResult,我希望它是来自多个方法的GenericResponse。
我的合同:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
[WcfSerialization::DataContract(Name = "MyServiceResponse")]
public class MyServiceResponse : object
{
public MyServiceResponse()
{
}
[WcfSerialization::DataMember(Name = "Success", IsRequired = true, Order = 0)]
public bool Success { get; set; }
[WcfSerialization::DataMember(Name = "ErrorMessage", IsRequired = true, Order = 1)]
public string ErrorMessage { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的界面:
[OperationContract()]
[WebInvoke(Method = "POST",
UriTemplate = "MyMethod",
BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json
)]
MyServiceResponse MyMethod(MyRequest requestData);
[OperationContract()]
[WebInvoke(Method = "POST",
UriTemplate = "MyMethod2",
BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json
)]
MyServiceResponse MyMethod2(MyRequest requestData);
Run Code Online (Sandbox Code Playgroud)
我期望,因为我已经用名称“ MyServiceResult”修饰了方法结果的数据协定,那么那将是结果json对象的名称,而不是为每个方法请求获取一个不同的名称。例如,代替:
{"MyServiceResponse":{"Success":true,"ErrorMessage":""}}
Run Code Online (Sandbox Code Playgroud)
通过电线我得到:
{"Method1Result":{"Success":true,"ErrorMessage":""}}
Run Code Online (Sandbox Code Playgroud)
和
{"Method2Result":{"Success":true,"ErrorMessage":""}}
Run Code Online (Sandbox Code Playgroud)
这样可以防止客户对结果进行一般检查,例如
success: function (returnData, textStatus, xhr) {
result.success = returnData.MyServiceResponse.Success;
result.errorMessage = returnData.errorMessage;
},
Run Code Online (Sandbox Code Playgroud)
谢谢
您可以向您的方法添加其他属性,以指定包装器的名称:
[return: MessageParameter(Name = "MyServiceResponse")]
Run Code Online (Sandbox Code Playgroud)
有关JSON REST格式的详细信息,请参阅RESTful Web服务主体格式问题。