问题描述
当我将它调用HTTP响应415时尝试将json字典发布到C#WCF.有人可以告诉我我的代码有什么问题.
trying to POST json dictionary to C# WCF, when i invoke it HTTP Response 415. Someone can tell me whats wrong with my code.
对象类
[DataContract]
public class Class1
{
[DataMember]
public string AccNo;
[DataMember]
public string CompanyName;
[DataMember]
public string DocDate;
}
IService1.cs
IService1.cs
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "json/PostSalesOrderData", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
string PostSalesOrderData(string data);
Service1.svc.cs
Service1.svc.cs
public string PostSalesOrderData(string data)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, Class1> dict = serializer.Deserialize<Dictionary<string, Class1>>(data);
return dict["Debtor"].AccNo.ToString();
}
小提琴细节
HTTP/1.1 415无法处理消息,因为内容类型为'application/json;charset = utf-8'不是预期的类型'text/xml;charset = utf-8'.伺服器:Microsoft-IIS/7.5X-Powered-by:ASP.NET日期:2012年11月29日,星期四,格林尼治标准时间内容长度:0
HTTP/1.1 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'.Server: Microsoft-IIS/7.5X-Powered-By: ASP.NETDate: Thu, 29 Nov 2012 01:21:55 GMTContent-Length: 0
推荐答案
服务的端点未正确配置为接收JSON输入.为了兑现 [WebInvoke]
属性,您的端点需要具有 webHttpBinding
,并且端点行为还应具有< webHttp类型/>
The endpoint for your service is not properly configured to receive JSON input. In order for the [WebInvoke]
attribute to be honored, your endpoint needs to have the webHttpBinding
, and it should also have an endpoint behavior of type <webHttp/>
一种确保正确配置的简单方法是使用.svc文件上的 Factory
属性.类似于下面的示例:
One easy way to ensure that it's properly configured is to use the Factory
attribute on the .svc file. Something like the example below:
<%@ ServiceHost Language="C#" Debug="true"
Service="YourNamespace.YourServiceClass"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
这篇关于HTTP/1.1 415无法处理消息,因为内容类型为'application/json;charset = utf-8'不是预期的类型'text/xml;charset = utf-8'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!