asp.net core 2.0 Json結果的格式

asp.net core 2.0 默認返回的結果格式是Json, 並使用json.net對結果默認作了camel case的轉化(大概可理解爲首字母小寫). web

這一點與老.net web api 不同, 原來的 asp.net web api 默認不適用任何NamingStrategy, 須要手動加上camelcase的轉化.json

若是非得把這個規則去掉, 那麼就在configureServices裏面改一下:api

        public void ConfigureServices(IServiceCollection services)
        { 
            services.AddMvc()
                .AddJsonOptions(options =>
                {
                    if (options.SerializerSettings.ContractResolver is DefaultContractResolver resolver)
                    {
                        resolver.NamingStrategy = null;
                    }
                });
        }

 修改前: asp.net

[{"id":1,"name":"牛奶","price":2.5},{"id":2,"name":"麪包","price":4.5}]
修改後:
[{"Id":1,"Name":"牛奶","Price":2.5},{"Id":2,"Name":"麪包","Price":4.5}]其實我更喜歡默認的....
相關文章
相關標籤/搜索