CustomizeJson.NETSerializerSettingsforEachType我正在使用ApiController,它使用全局HttpConfiguration类来指定JsonFormatter设置。我可以非常轻松地在全局范围内设置序列化设置:config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling=PreserveReferencesHandling.Objects;问题是并非所有设置都适用于我项目中的所有类型。我想为执行多态序列化的特定类型指定自定义TypeNameHandling和Binder选项。如何在每个类型或至少每个ApiController上指定JsonFormatter.SerializationSettings?根据您上面的评论,这里是每个控制器配置的示例:以上是C#学习教程:每种类型的自定义Json.NET序列化器设置分享全部内容,如果对大家有用还需要了解更多关于C#学习教程,希望大家多多关注——[MyControllerConfig]publicclassValuesController:ApiController[AttributeUsage(AttributeTargets.Class,AllowMultiple=false,Inherited=true)]publicclassMyControllerConfigAttribute:Attribute,IControllerConfiguration{publicvoidInitializationControllerSetting(SettpControllerSetting,HttpControllerScriptcontrollerDescriptor){//删除现有的Json格式化程序,因为这是全局格式化程序,更改它的任何设置//也会影响其他控制器。controllerSettings.Formatters.Remove(controllerSettings.Formatters.JsonFormatter);JsonMediaTypeFormatter格式化程序=newJsonTypeFormatter(;formatter.SerializerSettings.PreserveReferencesHandling=PreserveReferencesHandling.All;controllerSettings.Formatters.Insert(0,格式叔);}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
