.NetCore Web Api 利用ActionFilterAttribute统一接口返回值格式及问题解析

2022-04-17 12:21:00

.Net Core 同 Asp.Net MVC一样有几种过滤器,这里不再赘述每个过滤器的执行顺序与作用。

在实际项目开发过程中,统一API返回值格式对前端或第三方调用将是非常必要的,在.NetCore中我们可以通过ActionFilterAttribute来进行统一返回值的封装。

在封装之前我们需要考虑下面几个问题:

1,需要对哪些结果进行封装

我目前的做法是,只对ObjectResult进行封装,其他的类型:FileResult,ContentResult,EmptyResult,RedirectResult不予处理

2,对异常错误的封装

既然是统一返回值,当然也要考虑接口异常的问题了

但是不是所有的异常我们都需要返回给前端的,我们可能需要自定义一个业务异常,业务异常可以在前端进行友好提示,系统异常完全没必要抛出给前端或第三方,且需要对系统异常进行日志记录

项目结构:

//GlobalExceptionFilterAttribute构造中注入其他服务,需要通过ServiceFilter添加 ops.Filters.Add(new Microsoft.AspNetCore.Mvc.ServiceFilterAttribute(typeof(Filters.GlobalExceptionFilterAttribute))); }); //注册GlobalExceptionFilterAttribute services.AddScoped<Filters.GlobalExceptionFilterAttribute>(); www.easck.com // 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    {        /// <summary>        /// string        /// </summary>        /// <returns></returns>        [HttpGet]        public string Index() => "Welecome to .NetCore";        /// 跳转,不处理        [HttpGet("redirect")]        public ActionResult Redirect() => RedirectToAction("Index");        ///        [HttpGet("num")]        public int Num() => 10;        /// 异步        [HttpGet("async")]        public Task<IEnumerable<string>> TaskString() =>Task.FromResult<IEnumerable<string>>(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");    }}

下面是返回结果截图:

.NetCoreWebApi利用ActionFilterAttribute统一接口返回值格式及问题解析

上图:访问/api/home和/apiwww.easck.com/home/redirect的结果

.NetCoreWebApi利用ActionFilterAttribute统一接口返回值格式及问题解析

上图:Action返回数字的结果

.NetCoreWebApi利用ActionFilterAttribute统一接口返回值格式及问题解析

上图:返回string集合的结果

.NetCoreWebApi利用ActionFilterAttribute统一接口返回值格式及问题解析

上图:输出文本文件的结果

.NetCoreWebApi利用ActionFilterAttribute统一接口返回值格式及问题解析

上图:返回ContentResult的结果

.NetCoreWebApi利用ActionFilterAttribute统一接口返回值格式及问题解析

上图:系统异常的结果,输出状态码为500

.NetCoreWebApi利用ActionFilterAttribute统一接口返回值格式及问题解析

上图:抛出业务异常的结果,输出状态码200

不知道如何上传ZIP包,实例代码项目已经放到Gitee上了,后面有时间也会写点简单的例子
地址:https://gitee.com/tang3402/net-core-samples.git