Best way to filter domain objects for json output in an ASP.NET MVC application

后端 未结 6 1251
温柔的废话
温柔的废话 2020-12-05 20:59

If I\'m rendering a regular view in asp.net mvc the only domain object properties that show up in my page the ones I specifically write out. For example:

<         


        
6条回答
  •  天涯浪人
    2020-12-05 21:53

    You could use the Newtonsoft library and [JsonIgnore] attribute for marking properties of your class you don't want to expose. There are other libraries (with possible different property ignore attribute name), I personally prefer this one since it's very flexible in JSON converter extensions etc + it can easily serialize anonymous objects.

    public class Customer
    {
        ...
        [JsonIgnore]
        public string UrlIn { get; set; }
        public string FirstName { get; set; }
        // following example of a converter, you could write your own as well
        [JsonConverter(typeof(Newtonsoft.Json.Converters.JavaScriptDateTimeConverter))]
        public DateTime Created { get { return _created; } }
    }
    

提交回复
热议问题