From f8e583c4fc69d10c7e8d22015f3afe69832ff11c Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 16 Jan 2021 10:33:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=BF=94=E5=9B=9E=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NX.ERP.WebAPi/App_Start/WebApiConfig.cs | 9 ++++++--- NX.ERP.WebAPi/NX.ERP.WebAPi.csproj | 6 ++++++ NX.ERP.WebAPi/packages.config | 2 ++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/NX.ERP.WebAPi/App_Start/WebApiConfig.cs b/NX.ERP.WebAPi/App_Start/WebApiConfig.cs index e6f4c3b..a1bf8c2 100644 --- a/NX.ERP.WebAPi/App_Start/WebApiConfig.cs +++ b/NX.ERP.WebAPi/App_Start/WebApiConfig.cs @@ -10,6 +10,7 @@ using System.Threading; using System.Threading.Tasks; using System.Web.Http; using System.Web.Http.Controllers; +using System.Web.Http.Cors; using System.Web.Http.Filters; namespace NX.ERP.WebAPi @@ -48,7 +49,7 @@ namespace NX.ERP.WebAPi //请求是否成功 // result.IsSuccess = actionExecutedContext.ActionContext.Response.IsSuccessStatusCode; //结果转为自定义消息格式 - HttpResponseMessage httpResponseMessage = new HttpResponseMessage() { Content = new StringContent(JsonConvert.SerializeObject(result, GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings)) }; + HttpResponseMessage httpResponseMessage = new HttpResponseMessage() { Content = new ObjectContent>(result, new JsonMediaTypeFormatter()) }; // 重新封装回传格式 actionExecutedContext.Response = httpResponseMessage; } @@ -88,7 +89,8 @@ namespace NX.ERP.WebAPi { //忽略循环引用,如果设置为Error,则遇到循环引用的时候报错 ReferenceLoopHandling = ReferenceLoopHandling.Ignore, - Formatting = Newtonsoft.Json.Formatting.Indented, + NullValueHandling = NullValueHandling.Ignore, + //Formatting = Newtonsoft.Json.Formatting.Indented, //小驼峰命名法 //ContractResolver = new DefaultContractResolver() //{ @@ -101,7 +103,7 @@ namespace NX.ERP.WebAPi //NullValueHandling = NullValueHandling.Ignore }; - + config.EnableCors(new EnableCorsAttribute("*", "*", "*")); // Web API 路由 config.MapHttpAttributeRoutes(); @@ -111,6 +113,7 @@ namespace NX.ERP.WebAPi routeTemplate: "api/{controller}/{action}/{code}", defaults: new { code = RouteParameter.Optional } ); + } } } diff --git a/NX.ERP.WebAPi/NX.ERP.WebAPi.csproj b/NX.ERP.WebAPi/NX.ERP.WebAPi.csproj index cb6db1f..b1e5a60 100644 --- a/NX.ERP.WebAPi/NX.ERP.WebAPi.csproj +++ b/NX.ERP.WebAPi/NX.ERP.WebAPi.csproj @@ -60,12 +60,18 @@ + + ..\packages\Microsoft.AspNet.Cors.5.2.7\lib\net45\System.Web.Cors.dll + + + ..\packages\Microsoft.AspNet.WebApi.Cors.5.2.7\lib\net45\System.Web.Http.Cors.dll + diff --git a/NX.ERP.WebAPi/packages.config b/NX.ERP.WebAPi/packages.config index ff65773..5e16158 100644 --- a/NX.ERP.WebAPi/packages.config +++ b/NX.ERP.WebAPi/packages.config @@ -2,6 +2,7 @@ + @@ -13,6 +14,7 @@ +