.net6的webapi项目统一封装返回值

cs 复制代码
/// <summary>
/// 封装工厂后台的返回数据
/// </summary>
public class FactoryResultFilterAttribute : ResultFilterAttribute
{
    /// <summary>
    /// 如果只是返回Task的接口,会自动封装成CommonResponse
    /// </summary>
    /// <param name="context"></param>
    /// <param name="next"></param>
    /// <returns></returns>
    public override async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
    {
        if (!context.ActionDescriptor.EndpointMetadata.Any(x => x.GetType().Equals(typeof(NoResultFilterAttribute))))
        {
            if (context.Result.GetType() == typeof(EmptyResult))
            {
                var res = new CommonResponse();
                context.Result = new JsonResult(res)
                {
                    ContentType = "application/json",
                    StatusCode = 200,
                };
            }
            else if ((context.HttpContext.Request.Method.Equals("GET", StringComparison.OrdinalIgnoreCase) || context.HttpContext.Request.Method.Equals("POST", StringComparison.OrdinalIgnoreCase)) && context.Result is ObjectResult)
            {
                var result = (ObjectResult)context.Result;
                var res = new CommonResponse<Object>
                {
                    Data = result.Value,
                };
                context.Result = new JsonResult(res)
                {
                    ContentType = "application/json",
                    StatusCode = 200,
                };
            }
        }
        await next.Invoke();
    }

}
  1. 统一包装返回参数,使用过滤器ResultFilterAttribute来实现,OnResultExecutionAsync中可以处理所有Get和Post类型接口返回的数据,用CommonResponse类包装一下。

CommonResponse类主要用来返回Code(200表示成功)和Message(访问失败的原因)

  1. 这里配合ExceptionFilterAttribute一起用,用来捕捉错误BusinessException,这样业务代码出现错误后,直接throw new BusinessException("金额不能为0"),就会被过滤器自动捕捉并封装成CommonResponse返回给前端,方便前端统一处理。
cs 复制代码
/// <summary>
/// 业务类型错误
/// </summary>
public class BusinessException : Exception
{
    public BusinessException() : base("数据不存在")
    {
    }
    public BusinessException(string message) : base(message)
    {

    }
}
cs 复制代码
/// <summary>
/// 通用的异常处理
/// </summary>
public class FactoryExceptionFilterAttribute : ExceptionFilterAttribute
{
    private readonly ILogger<FactoryExceptionFilterAttribute> _logger;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="logger"></param>
    public FactoryExceptionFilterAttribute(ILogger<FactoryExceptionFilterAttribute> logger)
    {
        _logger = logger;
    }
    /// <summary>
    /// 通用api接口异常处理
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    public override Task OnExceptionAsync(ExceptionContext context)
    {
        if (!context.ExceptionHandled)
        {
            Exception exception = context.Exception;
            if (exception is BusinessException)
            {
                context.Result = new JsonResult(new CommonResponse(exception.Message));
            }
            else if (exception is ValidationException)
            {
                ValidationException? e = exception as ValidationException;
                context.Result = new JsonResult(new CommonResponse(e?.Errors.FirstOrDefault()?.ErrorMessage ?? ""));
            }
            else
            {
                var res = new CommonResponse
                {
                    Code = ResCode.ServerError,
                    Message = "请求错误"
                };
                exception.Log(context.HttpContext);
                _logger.LogError(exception.Message);
                context.Result = new JsonResult(res);
            }
        }
        context.ExceptionHandled = true;
        return Task.CompletedTask;
    }
}
相关推荐
暖馒5 小时前
Modbus应用层协议的深度剖析
网络·网络协议·c#·wpf·智能硬件
刘欣的博客8 小时前
C# CS架构程序发版升级的走数据库方案
c#·单文件升级自己的方式
Yorlen_Zhang10 小时前
Python Tkinter Text 控件完全指南:从基础编辑器到富文本应用
开发语言·python·c#
不绝19110 小时前
C#进阶:预处理指令/反射,Gettype,Typeof/关键类
开发语言·c#
大鹏说大话10 小时前
告别 MSBuild 脚本混乱:用 C# 和 Nuke 构建清晰、可维护的现代化构建系统
开发语言·c#
czhc114007566312 小时前
通信 28
c#
永远是我的最爱14 小时前
基于.NET的小小便利店前台收银系统
前端·sqlserver·.net·visual studio
菜鸟特工00715 小时前
javax.net.ssl.SSLPeerUnverifiedException 异常如何处理
网络协议·.net·ssl
bugcome_com15 小时前
C# 程序结构详解:从 Hello World 开始
c#
唐梓航-求职中16 小时前
编程-技术-算法-leetcode-288. 单词的唯一缩写
算法·leetcode·c#