How to set the EmitDefaultValue to false globally

2019-06-22 05:19发布

I'm using Web API and I've set the below property so that the default value is not displayed when members are serialized:

 [DataMember(EmitDefaultValue = false)]
        public string EventName { get; set; }

I have in total about 20 DataContracts and 100 properties as DataMembers so how would that be possible to set the EmitDefaultValue to false globally for all?

I hope I don't have to write my own logic using Reflection!

1条回答
Juvenile、少年°
2楼-- · 2019-06-22 05:48

In your App_Start folder add this to WebApiConfig

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Formatters.JsonFormatter.SerializerSettings =
            new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
查看更多
登录 后发表回答