.NetCore Web Api 利用ActionFilterAttribute统一接口返回值格式
.Net Core 同 Asp.Net MVC一样有几种过滤器,这里不再赘述每个过滤器的执行顺序与作用。
在实际项目开发过程中,统一API返回值格式对前端或第三方调用将是非常必要的,在.NetCore中我们可以通过ActionFilterAttribute来进行统一返回值的封装。
在封装之前我们需要考虑下面几个问题:
1,需要对哪些结果进行封装
我目前的做法是,只对ObjectResult进行封装,其他的类型:FileResult,ContentResult,EmptyResult,RedirectResult不予处理
2,对异常错误的封装
既然是统一返回值,当然也要考虑接口异常的问题了
但是不是所有的异常我们都需要返回给前端的,我们可能需要自定义一个业务异常,业务异常可以在前端进行友好提示,系统异常完全没必要抛出给前端或第三方,且需要对系统异常进行日志记录
项目结构:
Exceptions:自定义业务异常
Filters:自定义过滤器(统一结果封装,全局异常)
Models:统一结果实体
部分代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | using System; namespace NetCoreCommonResult.Exceptions { /// <summary> /// 自定义业务异常,可以由前端抛出友好的提示 /// </summary> public class BizException:Exception { public BizException() { } public BizException( string message): base (message) { } public BizException( string message, Exception ex) : base (message, ex) { } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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 }); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Logging; namespace NetCoreCommonResult.Filters { public class GlobalExceptionFilterAttribute : ExceptionFilterAttribute { private readonly ILogger<GlobalExceptionFilterAttribute> _logger; public GlobalExceptionFilterAttribute(ILogger<GlobalExceptionFilterAttribute> 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 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<Filters.GlobalExceptionFilterAttribute>(); } // 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | 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 { /// <summary> /// string /// </summary> /// <returns></returns> [HttpGet] public string Index() => "Welecome to .NetCore" ; /// <summary> /// 跳转,不处理 /// </summary> /// <returns></returns> [HttpGet( "redirect" )] public ActionResult Redirect() => RedirectToAction( "Index" ); /// <summary> /// /// </summary> /// <returns></returns> [HttpGet( "num" )] public int Num() => 10; /// <summary> /// 异步 /// </summary> /// <returns></returns> [HttpGet( "async" )] public Task<IEnumerable< string >> TaskString() =>Task.FromResult<IEnumerable< string >>( new [] { "A" , "B" , "C" }); /// <summary> /// 文件输出,不处理 /// </summary> /// <returns></returns> [HttpGet( "file" )] public ActionResult GetFile() => File(Encoding.UTF8.GetBytes( "File String" ), "text/plain" ); /// <summary> /// 空返回值,不处理 /// </summary> /// <returns></returns> [HttpGet( "empty" )] public ActionResult Empty() => Empty(); /// <summary> /// contentResult 不处理 /// </summary> /// <returns></returns> [HttpGet( "content" )] public ActionResult Content() => Content( "this is content" ); /// <summary> /// 异常,返回500错误 /// </summary> /// <returns></returns> [HttpGet( "exception" )] public ActionResult GetException() => throw new InvalidOperationException( "invalid" ); /// <summary> /// 自定义异常,返回200 /// </summary> /// <returns></returns> [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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!