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

推荐订阅源

WordPress大学
WordPress大学
小众软件
小众软件
MongoDB | Blog
MongoDB | Blog
Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
大猫的无限游戏
大猫的无限游戏
量子位
A
About on SuperTechFans
G
Google Developers Blog
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园_首页
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
U
Unit 42
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - 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);
    }
}