WebApi about configuring global return Json data format time and naming lowercase

1. Configure directly in the Global file:

copy code
  1 var formatters = GlobalConfiguration.Configuration.Formatters;
  2 var jsonFormatter = formatters.JsonFormatter;
  3 var settings = jsonFormatter.SerializerSettings;
  4 settings.Formatting = Newtonsoft.Json.Formatting.Indented;
  5 settings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
  6 settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
copy code
View Code

image

image

2. Configure any return format json and xml

  1config.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("format", "json", "application/json"));
  2config.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping("format", "xml", "application/xml"));
View Code

image

3. Use the Cors cross-domain framework to configure cross-domain

image

3.1 Configuration under the webapi controller

image

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325200956&siteId=291194637