【发布时间】:2017-12-10 12:04:54
【问题描述】:
我正在创建一个动作过滤器,它应该读取动作返回的响应,从中收集一些数据然后记录它,但是我找不到可以读取响应正文的任何地方。我尝试使用以下代码在 HttpContext 的响应中访问属性名称 OutputStream,但我不断收到错误消息,指出此流不支持 Length 属性和 Read 方法。
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
byte[] buffer = new byte[filterContext.HttpContext.Response.OutputStream.Length];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = filterContext.HttpContext.Response.OutputStream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
string html = Encoding.UTF8.GetString(ms.ToArray());
}
}
【问题讨论】:
-
不,很遗憾。
-
我也试过这个 Filter 属性,但遇到了同样的问题。这个问题是写给响应而不是阅读它。
标签: c# html asp.net-mvc response httpcontext