首页 > Windows开发 > 详细

web api 简单配置

时间:2019-12-01 18:00:54      阅读:84      评论:0      收藏:0      [点我收藏+]

public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API 配置和服务
#region Newtonsoft.json替换为Json
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings
{
//忽略循环引用,如果设置为Error,则遇到循环引用的时候报错
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Formatting = Newtonsoft.Json.Formatting.Indented,
//小驼峰命名法
ContractResolver = new CamelCasePropertyNamesContractResolver(),
//格式化日期时间
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
DateFormatString = "yyyy-MM-dd HH:mm:ss",
//空值处理
NullValueHandling = NullValueHandling.Ignore

};
#endregion

#region enable CORS
EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*"); //http://localhost:13713
config.EnableCors(cors);
#endregion

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);

// Web API 路由
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{phoneNum}",
defaults: new { phoneNum = RouteParameter.Optional }
);
}
}

web api 简单配置

原文:https://www.cnblogs.com/yulin10/p/11966605.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!