I'm working on a WebApi project and I want to serialize a List<User>
and send it back to the user. But it just returns [{"$id":"1"}]
where User
is an entity which was generated by Entity Framework database-first models classes.
Controller:
public List<User> GetAllUsers()
{
List<User> Users = Common.Framework.Persistence.PersistSvr<Business.Entity.User>
.GetAll().ToList();
return Users;
}
Here is the User
class:
public partial class User
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public User()
{
this.ContentLikes = new HashSet<ContentLikes>();
this.ContentPermission = new HashSet<ContentPermission>();
this.File = new HashSet<File>();
this.FolderPermission = new HashSet<FolderPermission>();
this.SiteUsers = new HashSet<SiteUsers>();
this.UserGroup = new HashSet<UserGroup>();
}
public long ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public Nullable<long> DiskUsed { get; set; }
public Nullable<long> DiskUsage { get; set; }
public Nullable<bool> Status { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ContentLikes> ContentLikes { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ContentPermission> ContentPermission { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<File> File { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<FolderPermission> FolderPermission { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<SiteUsers> SiteUsers { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<UserGroup> UserGroup { get; set; }
}
I have added these lines of codes to WebApiConfig.cs File.
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
it just resolved double serialization. and i have my old problem.
i will receive $id : "1"
when i send a request to my ActionMethod.
i have learned that it's because of EntityFramework. because WebApi doesn't able to serialize an EntityFramework Entity (i think it's because of relations that exists in my Models)
so what should i do to solve it ? I don't want to rewrite them all.
> GetAllUsers() { List Users = Common.Framework.Persistence.PersistSvr.GetAll().ToList();
return Json(Users);
}
– RezaNoei May 29 '18 at 17:38