我有一个 ASP.NET 中间件,我只想在不使用 YARP 转发时运行它。
有没有办法通过 headers 或其他任何东西来检测当前请求是否是通过 YARP 转发的?
您可以使用 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;
});
});