[英]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'
trying to POST json dictionary to C# WCF, when i invoke it HTTP Response 415. Someone can tell me whats wrong with my code. 尝试将json字典发布到C#WCF,当我调用HTTP响应415时。有人可以告诉我我的代码有什么问题。
object Class 对象类
[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();
}
Fiddle Details 小提琴细节
HTTP/1.1 415 Cannot process the message because the content type 'application/json; HTTP / 1.1 415无法处理消息,因为内容类型为'application / json; charset=utf-8' was not the expected type 'text/xml; charset = utf-8'不是预期的类型'text / xml; charset=utf-8'. 字符集= UTF-8' 。 Server: Microsoft-IIS/7.5 X-Powered-By: ASP.NET Date: Thu, 29 Nov 2012 01:21:55 GMT Content-Length: 0 服务器:Microsoft-IIS / 7.5 X-Powered-By:ASP.NET日期:星期四,2012年11月29日01:21:55 GMT内容长度:0
The endpoint for your service is not properly configured to receive JSON input. 您的服务端点未正确配置为接收JSON输入。 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/>
为了使[WebInvoke]
属性得到遵守,您的端点需要具有webHttpBinding
,并且它还应具有类型为<webHttp/>
的端点行为
One easy way to ensure that it's properly configured is to use the Factory
attribute on the .svc file. 确保正确配置的一种简单方法是使用.svc文件上的Factory
属性。 Something like the example below: 类似下面的例子:
<%@ ServiceHost Language="C#" Debug="true"
Service="YourNamespace.YourServiceClass"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.