【发布时间】:2017-02-01 10:42:03
【问题描述】:
我有一个相当简单的控制器。我的错误是“application/xml”,不像重定向给我的问题是“application/json”。
public class ProductController : ApiController
{
private IProductService productservice = new ProductServices();
public List<Employee> Get()
{
return productservice.GetProducts();
}
}
}
但我仍然收到以下错误。
“ObjectContent`1”类型无法序列化内容类型“application/xml;”的响应正文; charset=utf-8'。
我尝试了以下方法,但没有成功。
-
在 WebApiConfig.cs 中添加了这个
var json = config.Formatters.JsonFormatter; json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects; config.Formatters.Remove(config.Formatters.XmlFormatter); -
在 Global.asax.cs 中添加这个
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter); GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;
【问题讨论】:
-
没有一个答案对我有用,而且我收到“应用程序/xml”错误
标签: http asp.net-web-api