zoukankan      html  css  js  c++  java
  • asp.net core 序列化字典时保留 key 的大小写

    重写 CamelCasePropertyNamesContractResolver 的 CreateDictionaryContract 方法

    stackoverflow

    public class CamelCaseExceptDictionaryKeysResolver : CamelCasePropertyNamesContractResolver
    {
        protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
        {
            JsonDictionaryContract contract = base.CreateDictionaryContract(objectType);
    
            contract.DictionaryKeyResolver = propertyName => propertyName;
    
            return contract;
        }
    }
    

    在 Startup 添加 MVC Json 配置

    service.AddMvc()
        .AddNewtonsoftJson(options => {
            options.SerializerSettings.ContractResolver = new CamelCaseExceptDictionaryKeysResolver();
        });
    

    这样序列化字典输出的 key 就会保留大小写

  • 相关阅读:
    SQL Server Management Studio
    uiimage拉伸
    时间空间复杂度
    冒泡选择排序

    插入排序
    快速构建APP
    TTTAtibutedlabel
    Git命令
    适配
  • 原文地址:https://www.cnblogs.com/zengande/p/14474511.html
Copyright © 2011-2022 走看看