通过HttpModule确定页面的大小

时间:2020-03-05 18:50:10  来源:igfitidea点击:

这对我们来说很简单:

我目前正在通过HttpModule记录请求持续时间,并且我想找出每个页面的字节数。

HttpContext.Current.Response.OutputStream.Length引发NotSupportedException`。

有什么简单的方法可以做到这一点?

解决方案

回答

我有一个实现流重写器的HttpModule。它从Stream类派生。在我的HttpModule中,我有以下代码:

void app_PreRequestHandlerExecute(object sender, EventArgs e)
{
    HttpResponse response = HttpContext.Current.Response;
    response.Filter = new MyRewriterStream(response.Filter);
}

在流类中,我有以下代码覆盖默认的Write方法:

public override void Write(byte[] buffer, int offset, int count)
{
     string outStr;
     outStr = UTF8Encoding.UTF8.GetString(buffer, offset, count);
     //Do useful stuff and write back to the stream
}

我们只需在第二点取字符串的长度