我正在使用ASP.NET MVC5WebApi。我想咨询我所有的用户。
我写了api/users,我收到了这样的信息:
“'ObjectContent`1‘类型无法序列化内容类型'application/json;字符集=utf-8’的响应正文”
在WebApiConfig中,我已经添加了以下几行:
HttpConfiguration config = new HttpConfiguration();
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; 但它仍然不起作用。
我的返回数据函数是这样的:
public IEnumerable<User> GetAll()
{
    using (Database db = new Database())
    {
        return db.Users.ToList();
    }
}发布于 2019-07-23 00:48:15
对类使用Serializable:
示例:
[Serializable]
public class UserModel {
    public string Name {get;set;}
    public string Age {get;set;}
}它对我起作用了!
https://stackoverflow.com/questions/23098191
复制相似问题