網站首頁 編程語言 正文
.Net Core 同 Asp.Net MVC一樣有幾種過濾器,這里不再贅述每個過濾器的執行順序與作用。
在實際項目開發過程中,統一API返回值格式對前端或第三方調用將是非常必要的,在.NetCore中我們可以通過ActionFilterAttribute來進行統一返回值的封裝。
在封裝之前我們需要考慮下面幾個問題:
1,需要對哪些結果進行封裝
我目前的做法是,只對ObjectResult進行封裝,其他的類型:FileResult,ContentResult,EmptyResult,RedirectResult不予處理
2,對異常錯誤的封裝
既然是統一返回值,當然也要考慮接口異常的問題了
但是不是所有的異常我們都需要返回給前端的,我們可能需要自定義一個業務異常,業務異常可以在前端進行友好提示,系統異常完全沒必要拋出給前端或第三方,且需要對系統異常進行日志記錄
項目結構:
Exceptions:自定義業務異常
Filters:自定義過濾器(統一結果封裝,全局異常)
Models:統一結果實體
部分代碼:
using System; namespace NetCoreCommonResult.Exceptions { ////// 自定義業務異常,可以由前端拋出友好的提示 /// public class BizException:Exception { public BizException() { } public BizException(string message):base(message) public BizException(string message, Exception ex) : base(message, ex) } }
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; namespace NetCoreCommonResult.Filters { public class CommonResultFilterAttribute : ActionFilterAttribute { public override void OnResultExecuting(ResultExecutingContext context) { if (context.Result is ObjectResult objRst) { if (objRst.Value is Models.ApiResult) return; context.Result = new ObjectResult(new Models.ApiResult { Success = true, Message = string.Empty, Data = objRst.Value }); } } } }
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Logging; namespace NetCoreCommonResult.Filters { public class GlobalExceptionFilterAttribute : ExceptionFilterAttribute { private readonly ILogger_logger; public GlobalExceptionFilterAttribute(ILogger logger) { _logger = logger; } public override void OnException(ExceptionContext context) { context.ExceptionHandled = true; var isBizExp = context.Exception is Exceptions.BizException; context.Result = new ObjectResult(new Models.ApiResult { Success = false, Message = context.Exception.Message }); //非業務異常記錄errorLog,返回500狀態碼,前端通過捕獲500狀態碼進行友好提示 if (isBizExp == false) { _logger.LogError(context.Exception, context.Exception.Message); context.HttpContext.Response.StatusCode = 500; } base.OnException(context); } } }
Startup.cs
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace NetCoreCommonResult { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) services.AddLogging(); services.AddControllers(ops => { //添加過濾器 ops.Filters.Add(new Filters.CommonResultFilterAttribute()); //GlobalExceptionFilterAttribute構造中注入其他服務,需要通過ServiceFilter添加 ops.Filters.Add(new Microsoft.AspNetCore.Mvc.ServiceFilterAttribute(typeof(Filters.GlobalExceptionFilterAttribute))); }); //注冊GlobalExceptionFilterAttribute services.AddScoped(); // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) if (env.IsDevelopment()) app.UseDeveloperExceptionPage(); } else app.UseExceptionHandler("/Error"); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => endpoints.MapControllers(); } }
最后新建一個Controller然后寫上幾個不同返回值的的Action
using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace NetCoreCommonResult.Controllers { [Route("api/[controller]")] [ApiController] public class HomeController : ControllerBase { ////// string /// ///[HttpGet] public string Index() => "Welecome to .NetCore"; /// 跳轉,不處理 [HttpGet("redirect")] public ActionResult Redirect() => RedirectToAction("Index"); /// [HttpGet("num")] public int Num() => 10; /// 異步 [HttpGet("async")] public Task > TaskString() =>Task.FromResult >(new[] { "A","B","C"}); /// 文件輸出,不處理 [HttpGet("file")] public ActionResult GetFile() => File(Encoding.UTF8.GetBytes("File String"), "text/plain"); /// 空返回值,不處理 [HttpGet("empty")] public ActionResult Empty() => Empty(); /// contentResult 不處理 [HttpGet("content")] public ActionResult Content() => Content("this is content"); /// 異常,返回500錯誤 [HttpGet("exception")] public ActionResult GetException() => throw new InvalidOperationException("invalid"); /// 自定義異常,返回200 [HttpGet("bizException")] public ActionResult GetBizException() => throw new Exceptions.BizException("bizException"); } }
下面是返回結果截圖:
上圖:訪問/api/home和/api/home/redirect的結果
上圖:Action返回數字的結果
上圖:返回string集合的結果
上圖:輸出文本文件的結果
上圖:返回ContentResult的結果
上圖:系統異常的結果,輸出狀態碼為500
上圖:拋出業務異常的結果,輸出狀態碼200
不知道如何上傳ZIP包,實例代碼項目已經放到Gitee上了,后面有時間也會寫點簡單的例子
地址:https://gitee.com/tang3402/net-core-samples.git
原文鏈接:https://www.cnblogs.com/263613093/p/15988515.html
相關推薦
- 2022-03-29 C語言的基本編寫規范你了解嗎_C 語言
- 2022-08-02 shell自動拉取鏡像并運行容器的shell腳本_linux shell
- 2022-09-12 Python中.py程序在CMD控制臺以指定虛擬環境運行_python
- 2022-01-19 webpack5+webpack-dev-server啟動項目熱更新失效/熱更新無效,webpack
- 2022-05-20 springCloud_Feign遠程調用
- 2022-09-26 符合選擇器和css三大特性組合
- 2022-08-20 C/C++多態深入探究原理_C 語言
- 2023-09-12 過擬合(over fit)和欠擬合(under fit)
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支