In my Web api project, right now I'm skipping null values. therefore return json ignores null values and prints the property.
In the Global.asax file:
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
However, I want to replace the null values with "-". but I do not want to use the data element attribute for each property ...
[DefaultValue("-")].
I have more than 10 classes in my project ... so this is not the most elegant solution.
I want this to be a simple solution and applicable to any conversion, as well as null values from Global.asax
Example.
public class User
{
public string user { get; set; }
public string name { get; set; }
public string dni { get; set; }
}
when all data exists, my return service
{
"user": "usertest",
"name": "nametest",
"dni": "123456789"
}
But when dni does not exist, answer that
{
"user": "usertest",
"name": "nametest",
"dni": ""
}
So, I would like to answer as follows
{
"user": "usertest",
"name": "nametest",
"dni": "-"
}