I have a list of strings in my web.config that describes the names of the model members (generated by Entity Framework) that are allowed to be serialized in my web api.
How do I eg set the [IgnoreDataMember]
attribute during runtime?
The idea is, that not all the data should be exposed and the configuration what should be exposed, should be configurable without recompilation.
So far, I am just setting all the values of the members not contained in that list to null
. But this solution is not optimal because eg members of type datetime
are serialized to "0001-01-01T00:00:00"
and in addition, the response contains a lot of unnecessary information (the responses can grow up to 150MB). So it would be nicer to simply remove the members from the serialization process.
You can use the attributes:
[XmlIgnore]
for XML or [JsonIgnore]
for JSON.
For example:
[XmlIgnore]
public string MyString { get; set; }
or
[JsonIgnore]
public string MyString { get; set; }
Hope this helps.
I worked around it with [DataMember(EmitDefaultValue = false)]
. So all my properties which are set to null are not included in the response. However, this is not the best solution because now I can't send any null values and swagger also shows the full model.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.