Force lowercase property names from Json() in ASP.NET MVC

前端 未结 5 1098
孤独总比滥情好
孤独总比滥情好 2020-11-29 18:43

Given the following class,

public class Result
{      
    public bool Success { get; set; }

    public string Message { get; set; }
}

I a

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 19:22

    You can add this setting to Global.asax, and it will be work everywhere.

    public class Global : HttpApplication
    {   
        void Application_Start(object sender, EventArgs e)
        {
            //....
             JsonConvert.DefaultSettings = () =>
             {
                 var settings = new JsonSerializerSettings
                 {
                     ContractResolver = new CamelCasePropertyNamesContractResolver(),
                     PreserveReferencesHandling = PreserveReferencesHandling.None,
                     Formatting = Formatting.None
                 };
    
                 return settings;
             }; 
             //....
         }
    }
    

提交回复
热议问题