C# 如何修改wcf服务方法的名称';s json结果
我试图找到有关如何修改从wcf服务返回的对象名称(json格式)的信息,该对象通过ajax调用返回给web客户机,而不是使用默认包装器。我一直在寻找相关的文章,但运气不好。结果的默认包装名称似乎是MethodNameResult,我希望它是来自多个方法的GenericResponse 我的合同:C# 如何修改wcf服务方法的名称';s json结果,c#,asp.net,json,wcf,C#,Asp.net,Json,Wcf,我试图找到有关如何修改从wcf服务返回的对象名称(json格式)的信息,该对象通过ajax调用返回给web客户机,而不是使用默认包装器。我一直在寻找相关的文章,但运气不好。结果的默认包装名称似乎是MethodNameResult,我希望它是来自多个方法的GenericResponse 我的合同: [System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("
[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; }
}
我的界面:
[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);
我希望,因为我已经用一个名称“MyServiceResult”修饰了方法结果的数据约定,所以这将是结果json对象的名称,而不是为每个方法请求获得不同的名称。例如,而不是:
{"MyServiceResponse":{"Success":true,"ErrorMessage":""}}
通过电线,我得到:
{"Method1Result":{"Success":true,"ErrorMessage":""}}
及
这会阻止客户对结果进行一般性检查,如
success: function (returnData, textStatus, xhr) {
result.success = returnData.MyServiceResponse.Success;
result.errorMessage = returnData.errorMessage;
},
谢谢尝试将您的
UriTemplate
设置设置为以下内容
UriTemplate = "MyServiceResponse"
。。。并将您的回复编辑为以下内容
public class MyServiceResponse
{
public bool Success { get; set; }
public string ErrorMessage { get; set; }
}
您正在为
SOAP
指定序列化,并添加许多C#将为您处理的内容,例如,类的默认构造函数。通过这种方式,您指定希望您的服务返回一个干净的JSON
对象,当字符串在线路上传输时,它将以“MyServiceResponse”开头。您可以向方法添加其他属性,指定包装器的名称:
[return: MessageParameter(Name = "MyServiceResponse")]
有关JSON REST格式的详细信息,请参考问题。谢谢,但该服务必须支持pox、JSON和soap结果。我正在努力保持响应对象的可互换性。谢谢,这正是我想要的!
[return: MessageParameter(Name = "MyServiceResponse")]