【发布时间】:2015-07-11 20:55:44
【问题描述】:
Visual Studio“Web API”项目模板包括用于处理用户注册、身份验证和授权的端点。然而,在生产应用程序中,用户通常也会与其他实体相关联,例如:
public class Post {
public Post() {};
public int Id { get; set; }
public ApplicationUser User { get; set; }
}
在这些情况下,ApplicationUser 类(派生自 IdentityUser)无法序列化。尝试这样做会产生类似于以下内容的错误:
“ObjectContent`1”类型无法序列化内容类型“application/json;”的响应正文; charset=utf-8'。
我在其他地方看到过类似的问题,建议通过 DTO 而不是 ApplicationUser 对象。然而,这似乎是很多开发人员的开销。有没有办法直接序列化ApplicationUser?
【问题讨论】:
标签: c# serialization entity-framework-6 asp.net-web-api2 asp.net-identity