检测 YARP 何时转发请求

问题描述 投票:0回答:1

我有一个 ASP.NET 中间件,我只想在不使用 YARP 转发时运行它。

有没有办法通过 headers 或其他任何东西来检测当前请求是否是通过 YARP 转发的?

asp.net-core request middleware yarp
1个回答
0
投票

您可以使用 Transforms 来检测转发事件。

builder.Services.AddReverseProxy()
            .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"))
                .AddTransforms(transformBuilderContext =>
                {
                    // Before forwarding the request to the destination
                    transformBuilderContext.AddRequestTransform(async transformContext =>
                    {
                        Console.WriteLine($"Forwarding request to: {transformContext.DestinationPrefix}");
                        await Task.CompletedTask;
                    });

                    // After receiving the response from the destination
                    transformBuilderContext.AddResponseTransform(async transformContext =>
                    {
                        Console.WriteLine($"Response received with status code: {transformContext.HttpContext.Response.StatusCode}");
                        await Task.CompletedTask;
                    });
                });
© www.soinside.com 2019 - 2024. All rights reserved.