From 5bc32e4b829cf617b0ed37b24845d9cefd0d1127 Mon Sep 17 00:00:00 2001 From: admin Date: Mon, 18 Jan 2021 09:02:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E7=A6=BB=E8=BF=87=E6=BB=A4=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NX.ERP.WebAPi/App_Start/WebApiConfig.cs | 40 ++------------------- NX.ERP.WebAPi/Filters/ApiResultAttribute.cs | 33 +++++++++++++++++ NX.ERP.WebAPi/Filters/ExceptionFilter.cs | 29 +++++++++++++++ NX.ERP.WebAPi/NX.ERP.WebAPi.csproj | 2 ++ 4 files changed, 66 insertions(+), 38 deletions(-) create mode 100644 NX.ERP.WebAPi/Filters/ApiResultAttribute.cs create mode 100644 NX.ERP.WebAPi/Filters/ExceptionFilter.cs diff --git a/NX.ERP.WebAPi/App_Start/WebApiConfig.cs b/NX.ERP.WebAPi/App_Start/WebApiConfig.cs index 22fa74e..2a87bb0 100644 --- a/NX.ERP.WebAPi/App_Start/WebApiConfig.cs +++ b/NX.ERP.WebAPi/App_Start/WebApiConfig.cs @@ -1,5 +1,6 @@ using Newtonsoft.Json; using Newtonsoft.Json.Serialization; +using NX.ERP.WebAPi.Filters; using System; using System.Collections.Generic; using System.Linq; @@ -16,45 +17,8 @@ using System.Web.Http.Filters; namespace NX.ERP.WebAPi { - public class ExceptionFilter : System.Web.Http.Filters.IExceptionFilter - { - public bool AllowMultiple => throw new NotImplementedException(); - public Task ExecuteExceptionFilterAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken) - { - var ex = actionExecutedContext.Exception; - actionExecutedContext.Response = new System.Net.Http.HttpResponseMessage(); - actionExecutedContext.Response.Content = new System.Net.Http.ObjectContent(typeof(ResultModel), - new ResultModel { ErrCode = 500, ErrMsg = ex.Message, Data = null }, - GlobalConfiguration.Configuration.Formatters.JsonFormatter); - - return Task.FromResult(0); - } - } - - public class ApiResultAttribute : System.Web.Http.Filters.ActionFilterAttribute - { - public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) - { - base.OnActionExecuted(actionExecutedContext); - if (actionExecutedContext.Exception == null) - { - ResultModel result = new ResultModel(); - // 取得由 API 返回的状态代码 - result.ErrCode = 0; - // 取得由 API 返回的资料 - result.Data = actionExecutedContext.ActionContext.Response.Content.ReadAsAsync().Result; - - //请求是否成功 - //result.IsSuccess = actionExecutedContext.ActionContext.Response.IsSuccessStatusCode; - //结果转为自定义消息格式 - actionExecutedContext.Response.Content = new ObjectContent>( result, GlobalConfiguration.Configuration.Formatters.JsonFormatter); - // 重新封装回传格式 - // actionExecutedContext.Response = httpResponseMessage; - } - } - } @@ -89,7 +53,7 @@ namespace NX.ERP.WebAPi //System.Web.Http.Filters.IExceptionFilter config.Filters.Add(new ExceptionFilter()); - //config.Filters.Add(new ApiResultAttribute()); + config.Filters.Add(new ApiResultAttribute()); Console.WriteLine(config.Filters.Count); // Web API 配置和服务 GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings diff --git a/NX.ERP.WebAPi/Filters/ApiResultAttribute.cs b/NX.ERP.WebAPi/Filters/ApiResultAttribute.cs new file mode 100644 index 0000000..21b039a --- /dev/null +++ b/NX.ERP.WebAPi/Filters/ApiResultAttribute.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Web; +using System.Web.Http; +using System.Web.Http.Filters; + +namespace NX.ERP.WebAPi.Filters +{ + public class ApiResultAttribute : System.Web.Http.Filters.ActionFilterAttribute + { + public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) + { + base.OnActionExecuted(actionExecutedContext); + if (actionExecutedContext.Exception == null) + { + ResultModel result = new ResultModel(); + // 取得由 API 返回的状态代码 + result.ErrCode = 0; + // 取得由 API 返回的资料 + result.Data = actionExecutedContext.ActionContext.Response.Content.ReadAsAsync().Result; + + //请求是否成功 + //result.IsSuccess = actionExecutedContext.ActionContext.Response.IsSuccessStatusCode; + //结果转为自定义消息格式 + actionExecutedContext.Response.Content = new ObjectContent>(result, GlobalConfiguration.Configuration.Formatters.JsonFormatter); + // 重新封装回传格式 + // actionExecutedContext.Response = httpResponseMessage; + } + } + } +} \ No newline at end of file diff --git a/NX.ERP.WebAPi/Filters/ExceptionFilter.cs b/NX.ERP.WebAPi/Filters/ExceptionFilter.cs new file mode 100644 index 0000000..a206679 --- /dev/null +++ b/NX.ERP.WebAPi/Filters/ExceptionFilter.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using System.Web; +using System.Web.Http; +using System.Web.Http.Filters; + +namespace NX.ERP.WebAPi.Filters +{ + public class ExceptionFilter : System.Web.Http.Filters.IExceptionFilter + { + public bool AllowMultiple => throw new NotImplementedException(); + + public Task ExecuteExceptionFilterAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken) + { + var ex = actionExecutedContext.Exception; + actionExecutedContext.Response = new System.Net.Http.HttpResponseMessage(); + + actionExecutedContext.Response.Content = new System.Net.Http.ObjectContent(typeof(ResultModel), + new ResultModel { ErrCode = 500, ErrMsg = ex.Message, Data = null }, + GlobalConfiguration.Configuration.Formatters.JsonFormatter); + + return Task.FromResult(0); + } + } + +} \ No newline at end of file diff --git a/NX.ERP.WebAPi/NX.ERP.WebAPi.csproj b/NX.ERP.WebAPi/NX.ERP.WebAPi.csproj index 935cc8b..65d83e1 100644 --- a/NX.ERP.WebAPi/NX.ERP.WebAPi.csproj +++ b/NX.ERP.WebAPi/NX.ERP.WebAPi.csproj @@ -167,6 +167,8 @@ + + Global.asax