C# 从C中的OperationContext读取JSON格式的请求内容#
我创建了WCF RESTful服务,如下所示:C# 从C中的OperationContext读取JSON格式的请求内容#,c#,json,wcf,operationcontext,weboperationcontext,C#,Json,Wcf,Operationcontext,Weboperationcontext,我创建了WCF RESTful服务,如下所示: [OperationContract] [WebInvoke(Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Customer/{customerID}/profile")] string PutCustomerProfileData(string customerID);
[OperationContract]
[WebInvoke(Method = "PUT",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/Customer/{customerID}/profile")]
string PutCustomerProfileData(string customerID);
{<root type="object">
<customerID type="string">RC0064211</customerID>
<TermsAgreed type="string">true</TermsAgreed>
</root>}
public string PutCustomerVerificationData(string customerID, CustomerVerification customerVerification)
{
JavaScriptSerializer js = new JavaScriptSerializer();
string requestBody = js.Serialize(customerVerification);
string serviceResponse = bllCustomerDetails.PutCustomerVerificationData(customerID, requestBody).Replace("\"", "'");
return serviceResponse;
}
我正在使用Postman调试它,并在正文中传递JSON数据,如下所示:
[OperationContract]
[WebInvoke(Method = "PUT",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/Customer/{customerID}/profile")]
string PutCustomerProfileData(string customerID);
{<root type="object">
<customerID type="string">RC0064211</customerID>
<TermsAgreed type="string">true</TermsAgreed>
</root>}
public string PutCustomerVerificationData(string customerID, CustomerVerification customerVerification)
{
JavaScriptSerializer js = new JavaScriptSerializer();
string requestBody = js.Serialize(customerVerification);
string serviceResponse = bllCustomerDetails.PutCustomerVerificationData(customerID, requestBody).Replace("\"", "'");
return serviceResponse;
}
{“customerID”:“RC0064211”,“TermsAgreed”:“true”}
它在RequestMessage中返回的内容如下:
[OperationContract]
[WebInvoke(Method = "PUT",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/Customer/{customerID}/profile")]
string PutCustomerProfileData(string customerID);
{<root type="object">
<customerID type="string">RC0064211</customerID>
<TermsAgreed type="string">true</TermsAgreed>
</root>}
public string PutCustomerVerificationData(string customerID, CustomerVerification customerVerification)
{
JavaScriptSerializer js = new JavaScriptSerializer();
string requestBody = js.Serialize(customerVerification);
string serviceResponse = bllCustomerDetails.PutCustomerVerificationData(customerID, requestBody).Replace("\"", "'");
return serviceResponse;
}
{
RC0064211
真的
}
我想要这个JSON格式的请求主体。能给我吗?如果没有,我可以用什么方法为前面提到的
RequestMessage
创建JSON字符串?在要转换为JSON的成员变量上添加[DataMember] 事实上,我并不完全理解这个问题,但作为一个建议,我可能会建议:
您应该像这样为Json设计WebConfig
<services>
<service name="Your Service Name"
<endpoint address="" behaviorConfiguration="webHttp" binding="webHttpBinding"
bindingConfiguration="webHttpBindingWithJsonP" contract="YourProjectName">
</endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" />
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp />
</behavior>
</endpointBehaviors>
另外,您可以在Fiddler 4上尝试您的Web服务,您可以请求和响应JSON或您想要的内容。我尝试了
DataContract
和DataMember
,它对我很有效
下面是示例代码:
[OperationContract]
[WebInvoke(Method = "PUT",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/Customer/{customerID}/verification")]
string PutCustomerVerificationData(string customerID, CustomerVerification customerVerification);
}
[DataContract]
public class CustomerVerification
{
[DataMember]
public string PasswordHash { get; set; }
[DataMember]
public string PasswordSalt { get; set; }
}
然后,我将该DataContract转换为JSON字符串,并进一步使用它,如下所示:
[OperationContract]
[WebInvoke(Method = "PUT",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/Customer/{customerID}/profile")]
string PutCustomerProfileData(string customerID);
{<root type="object">
<customerID type="string">RC0064211</customerID>
<TermsAgreed type="string">true</TermsAgreed>
</root>}
public string PutCustomerVerificationData(string customerID, CustomerVerification customerVerification)
{
JavaScriptSerializer js = new JavaScriptSerializer();
string requestBody = js.Serialize(customerVerification);
string serviceResponse = bllCustomerDetails.PutCustomerVerificationData(customerID, requestBody).Replace("\"", "'");
return serviceResponse;
}
您可以尝试在Postman中添加
Accept:application/json
头。但老实说,这不可能是您的PutCustomerProfileData
的实际代码-您返回的是什么?我已经设置了该标题,但无论如何也帮不了我。对于PutCustomerProfileData
,它将只返回带有正确消息的JSON格式。PutCustomerProfileData
显然不会返回JSON,因为您看到的是XML。您能否粘贴PutCustomerProfileData
的完整代码(或至少与构建输出字符串相关的所有内容)。如果可能,您能否提供一些代码示例,因为我不知道如何使用DataMember
你提到的是同一个问题吗?不,大约6个月前我就遇到了这个问题,所以[DataMember]为我修复了这个问题。