ASP.Net Web API 项目基础配置
2018-11-28 本文已影响0人
JADDC
一、跨域、小驼峰配置
在App_Start 文件夹下的WebApiConfig.cs
添加如下代码
//Cors 跨域
var allowOrigins = AppSettings.CorsAllowOrigins;
var allowHeaders = AppSettings.CorsAllowHeaders;
var allowMethods = AppSettings.CorsAllowMethods;
var allowExposedHeaders = AppSettings.CorsAllowExposedHeaders;
var globalCors = new EnableCorsAttribute(allowOrigins, allowHeaders, allowMethods, allowExposedHeaders);
config.EnableCors(globalCors);
//小驼峰设置
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
在Web.config
的<appSettings>
节点中添加跨域信息
<!--Cors 跨域设置-->
<add key="CorsAllowOrigins" value="*" />
<add key="CorsAllowHeaders" value="*"/>
<add key="CorsAllowMethods" value="*"/>
<add key="CorsAllowExposedHeaders" value="*"/>
%AppData%\NuGet\