修改OwinMiddleware响应 [英] Modify OwinMiddleware response

查看:231
本文介绍了修改OwinMiddleware响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大约知道如何实例是OwinMiddleware链接。我发现了一堆涉及到如何通过阅读下一个中间件生成的响应内容的例子,我发现的例子涉及到如何修改响应头的值(例如的http://stackoverflow.com/a/17513249 ) - 但我找不到如何修改响应体的例子。请不要问为什么其实我是想做到这一点,是否有可能替代解决方案(是的,我知道还有的XY-问题):我们认为这是一个编码练习,请

I approximately understand how OwinMiddleware instances are chained. I have found a bunch of examples related to how to READ response content generated by "next" middlewares, and I have found examples related to how to modify response header values (e.g. http://stackoverflow.com/a/17513249) - but I cannot find an example of how to modify response body. Please don't ask why I actually want to do this and whether there may be alternative solutions (yes, I'm aware of XY-problem as well): let's consider it a coding exercise, please.

当然,我能理解,修改正文内容会影响内容长度的头;我也能理解,有些反应可能相互关联和修改它们会破坏这种关系。

Sure, I can understand that modifying body content affects content-length in the header; I can also understand that some responses may relate to each other and that modifying them will break that relation.

我试图使应对修改后的下一步回报,但没有改变正在对原始取得。如果没有内容留在服务器在这一点上,他很自然地我期望能够完全取代反应,无论怎么说的行为可能会被视为不友好的成分,明智的。

I tried making response changes AFTER the "next" returns, but no changes are being made to the original. If no content left the server at this point, it seems natural to me to expect to be able to completely replace the response, regardless of how that behavior may be construed as unfriendly component-wise.

感谢您。

推荐答案

一个例子是在这里:<一href=\"http://stackoverflow.com/questions/26214113/how-can-i-safely-intercept-the-response-stream-in-a-custom-owin-middleware\">How我可以放心地拦截响应流中的自定义Owin中间件

这只是不明确的,因为我需要它是我的目的,所以我不能让它在第一次工作。

It just wasn't as clear as I needed it to be for my purposes, so I couldn't make it work at first.

基本上,如果我想改变/替换的内容,我必须把它写入 context.Response.Body 流。我误把那个例子是这仅仅是如何阅读的反应,但你不能改变它,因为我得到了与车身流是如何取代,并在年底写入混淆。

Basically, if I want to change/replace the content, I have to write it to context.Response.Body stream. I mistakenly took that example as "this is only how to read response, but you can't change it" because I got confused with how the body-stream was replaced and written to at the end.

还有很重要的一点是, context.Response.ContentLength [绝对]也必须进行设置/更改,但显然只有当内容的大小( stream.Length )的变化(这是在我的情况真的)。

Another very important thing is that context.Response.ContentLength [absolutely] must also be set/changed, but obviously only if the size of the content (stream.Length) changes (which was true in my case).

这篇关于修改OwinMiddleware响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆