如果条件不满足,则“短路”YARP RequestTransform 并返回自定义响应

Posted

技术标签:

【中文标题】如果条件不满足,则“短路”YARP RequestTransform 并返回自定义响应【英文标题】:"Short-circuit" YARP RequestTransform if condition is not met and return custom response 【发布时间】:2021-12-06 03:49:32 【问题描述】:

如果在请求转换中不满足特定条件,我无法弄清楚如何提前停止 YARP 请求并返回自定义响应。

builderContext.AddRequestTransform(async transformContext =>

        [...]

        if (!condition)
        
                // TODO: stop request and return response
        

        [...]
);

我尝试像对中间件所做的那样简单地将 transformContext.HttpContext.Response.Status 设置为非成功状态代码,但这似乎不起作用,而且我无法找到解决此问题的其他方法.

【问题讨论】:

【参考方案1】:

也许您应该使用中间件。如果您查看文档,您可以使用中间件进行拦截。

https://microsoft.github.io/reverse-proxy/articles/middleware.html#adding-middleware

如果您在next() 之前返回,则不会调用代理。

【讨论】:

以上是关于如果条件不满足,则“短路”YARP RequestTransform 并返回自定义响应的主要内容,如果未能解决你的问题,请参考以下文章