HTTP / 1.1 415无法处理消息,因为内容类型为’application / json; charset = utf-8’不是预期的类型’text / xml; 字符集= UTF-8′

尝试将json字典发布到C#WCF,当我调用它时HTTP响应415.有人可以告诉我我的代码有什么问题。

对象类

[DataContract] public class Class1 { [DataMember] public string AccNo; [DataMember] public string CompanyName; [DataMember] public string DocDate; } 

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

  public string PostSalesOrderData(string data) { JavaScriptSerializer serializer = new JavaScriptSerializer(); Dictionary dict = serializer.Deserialize<Dictionary>(data); return dict["Debtor"].AccNo.ToString(); } 

小提琴细节

HTTP / 1.1 415无法处理消息,因为内容类型为’application / json; charset = utf-8’不是预期的类型’text / xml; 字符集= UTF-8′ 。 服务器:Microsoft-IIS / 7.5 X-Powered-By:ASP.NET日期:星期四,2012年11月29日01:21:55 GMT内容长度:0

您的服务端点未正确配置为接收JSON输入。 为了使[WebInvoke]属性得到遵守,您的端点需要具有webHttpBinding ,并且它还应具有类型为的端点行为

确保正确配置的一种简单方法是使用.svc文件上的Factory属性。 类似下面的例子:

 <%@ ServiceHost Language="C#" Debug="true" Service="YourNamespace.YourServiceClass" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>