如何将Web API中所有数据成员的EmitDefaultValue全局设置为false? [英] How to set the EmitDefaultValue to false globally for all Data Members in Web API?

查看:51
本文介绍了如何将Web API中所有数据成员的EmitDefaultValue全局设置为false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Web API,并且设置了以下属性,以便在序列化成员时不显示默认值:

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; }

我总共有大约20个DataContracts和100个属性作为DataMembers,那么如何将EmitDefaultValue全局设置为false?

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!

推荐答案

在您的App_Start文件夹中,将其添加到WebApiConfig

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 };

这篇关于如何将Web API中所有数据成员的EmitDefaultValue全局设置为false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆