惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
宝玉的分享
宝玉的分享
Jina AI
Jina AI
IT之家
IT之家
博客园 - Franky
MyScale Blog
MyScale Blog
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
雷峰网
雷峰网
WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
美团技术团队
S
SegmentFault 最新的问题
罗磊的独立博客
博客园 - 聂微东
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
D
Docker
博客园 - 司徒正美
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
M
MIT News - Artificial intelligence

博客园 - Hey,Coder!

微信小程序 web方案实现调音器功能 systemctl slice配置docker最大占用cpu 内存 pipenv环境变量配置 .net8验证码 数据库差异对比工具 docker部署prometheus grafana pgexporter Alertmanager ubuntu24 换源 ssh配置 ubuntu24 Harbor部署 ubuntu24 kvm部署 cockpit web管理界面 .net api代理 Ubuntu 24.04 DMAR IOMMU 报错修复 uniapp iOS App 上架 vs文件软链接 Docker 网络别名 .net环境下OpenTelemetry Body 读取注意事项 jaeger界面显示异常 .net opentelemetry collector jaeger c# opentelemetry 自定义export c# opentelemetry 自定义export c# 生成对象的初始化代码 Elasticsearch ILM 与 Data Stream 概念及实例说明文档 portainer httphelper封装 Ocelot + Consul + SignalR 粘性会话 正交软件架构 docker postgresql17 主从复制 rabbitmq总结与c#示例 docker rabbitmq quartz dashboard docker consul registrator 服务自动注册consul 微服务动态扩容
.net8通用中间件处理公共返回值结构
Hey,Coder! · 2026-07-27 · via 博客园 - Hey,Coder!
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Threading.Tasks;
using Thea;
using Thea.ExceptionEx;
using Thea.Web;

public class TheaWebMiddleware
{
    private readonly RequestDelegate next;

    public TheaWebMiddleware(RequestDelegate next)
    {
        this.next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        // SignalR 放行
        if (context.GetEndpoint()?.Metadata?.GetMetadata<SignalREndpointAttribute>() != null)
        {
            await next(context);
            return;
        }

        CommonResponse result = null;
        var statusCode = 200;

        try
        {
            await next(context);

            if (context.Response.StatusCode == 200)
            {
                if (context.Response.Body.Length != 0)
                {
                    //有数据时直接返回
                    //当前代码没有处理通用结构,此处直接返回即可
                    //如果需要处理通用结构则需要在此处增加
                    return;
                }
                else
                {
                    //无任何数据返回时返回通用的成功结构
                    result = CommonResponse.Success;
                }
            }
            else
            {
                statusCode = context.Response.StatusCode;
                result = CommonResponse.Fail(statusCode, statusCode.ToString());
            }
        }
        catch (Exception ex)
        {
            var exception = ex.InnerException ?? ex;
            result = BuildErrorResponse(exception, 200);
        }

        var json = JsonConvert.SerializeObject(result);
        context.Response.Clear();
        context.Response.StatusCode = statusCode;
        await context.Response.WriteAsJsonAsync(json);
    }

    private static CommonResponse BuildErrorResponse(Exception exception, int statusCode)
    {
        if (exception is LevelException levelEx)
        {
            var exConfig = LevelExceptionConfig.GetConfig(levelEx);
            return CommonResponse.Fail((int)ErrorLevel.LangageInfo, levelEx.Message, exConfig);
        }
        return CommonResponse.Fail(statusCode, exception.Message);
    }
}