相关疑难解决方法(0)

无法使用Json在Web API中序列化响应

我正在使用ASP.NET MVC 5 Web Api.我想咨询我的所有用户.

我写了api/users,我收到了这个:

"'ObjectContent`1'类型无法为内容类型'application/json; charset = utf-8'"序列化响应正文

在WebApiConfig中,我已添加以下行:

HttpConfiguration config = new HttpConfiguration();
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; 
Run Code Online (Sandbox Code Playgroud)

但它仍然无效.

我的返回数据函数是这样的:

public IEnumerable<User> GetAll()
{
    using (Database db = new Database())
    {
        return db.Users.ToList();
    }
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net json asp.net-web-api

102
推荐指数
9
解决办法
17万
查看次数

标签 统计

asp.net ×1

asp.net-web-api ×1

c# ×1

json ×1