C# c中WCF服务的裸Json响应#
我在.NET4平台中创建了一个WCF服务,当我用jQueryAjax帖子点击它时,它返回JSON。 我所遇到的问题是,我更希望POST的json响应不使用带有结果后缀的方法名称包装 详细内容:C# c中WCF服务的裸Json响应#,c#,asp.net,json,wcf,jquery,C#,Asp.net,Json,Wcf,Jquery,我在.NET4平台中创建了一个WCF服务,当我用jQueryAjax帖子点击它时,它返回JSON。 我所遇到的问题是,我更希望POST的json响应不使用带有结果后缀的方法名称包装 详细内容: public interface IService { [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodySt
public interface IService
{
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
Person GetInfo(string id);
}
[AspNetCompatibilityRequirements(RequirementsMode
= AspNetCompatibilityRequirementsMode.Allowed)]
public class Service: Iservice
{
public Person GetInfo(string id)
{
...
return new Person();
}
}
public class Person
{
public string FirstName;
public string LastName;
public Person(){
FirstName = "Jon";
LastName = "Doe";
}
}
web.config
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceBehavior" name="Service">
<endpoint address="" binding="webHttpBinding"
contract="IService" behaviorConfiguration="EndpBehavior"/>
</service>
</services>
</system.serviceModel>
使用BodyStyle=WebMessageBodyStyle.Wrapped
在我的javascript代码中得到的响应是:
{"GetInfoResult" : {"FirstName":"Jon", "LastName":"Doe"}}
但是当我将其更改为WebMessageBodyStyle.Bare
并且其他所有内容都保持不变时,会发生500内部服务器错误
在没有方法+结果包装器的情况下,是否可以在我的POST响应中返回裸json?如果是,我做错了什么
提前感谢我认为
WebMessageBodyStyle.Bare
也需要一个“Bare”请求,并且您正在从javascript代码发送一个对象,类似这样:{id:'value}
,它将被翻译为:
public class DTObject{
public string Id { get; set; }
}
并且您的操作方法只需要一个字符串
作为参数
尽量只让你的回答像这样“赤裸裸”WebMessageBodyStyle.WrappedRequest
[编辑]
public class DTObject{
public string Id { get; set; }
}