c# - Exclude certain fields when returning as json -
i have asp.net web api application.
for lets application consists of user entity , post entity. post written user, every post entity contains reference user entity.
class post { public int id { get; set; } public string title { get; set; } public string content { get; set; } public user user { get; set; } // reference user wrote post }
the problem when want return list of posts json. don't want include writers of posts inside list, in other words, want exclude user field list of posts.
example:
[ { "id": 1, "title": "post a", "content": "..." }, { "id": 2, "title": "post b", "content": "..." } ]
i know can creating new class called jsonpost without user field , converting list of post's list of jsonpost's linq, want solve without creating new class.
thanks, arik
just mark post's user property [jsonignore] attribute newtonsoft.json namespace , won't serialized
using newtonsoft.json; class post { public int id { get; set; } public string title { get; set; } public string content { get; set; } [jsonignore] public user user { get; set; } // property won't serialized }
Comments
Post a Comment