ASP.NET Core中返回 json 數據首字母大小寫問題


asp.net core  默認輸出的json數據首字母會改成小寫,考慮可能是為了前端js的書寫習慣,畢竟js 操作大小寫敏感。

在Startup類的ConfigureServices()方法中進行配置,DefaultContractResolver() 原樣輸出,返回的 json 與后台定義一致

 

// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
                
            });

            //options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();//json字符串大小寫原樣輸出
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
                //.AddJsonOptions(opt => { 
                //    opt.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
                //    });// json字符串大小寫原樣輸出


        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM