使用控制器操作过滤器捕获 HTML 输出
我在一个操作上设置了以下过滤器来捕获 HTML 输出,将其转换为字符串,执行一些操作来修改字符串,并返回带有新字符串的 ContentResult。不幸的是,我总是以空字符串结束。
private class UpdateFilter : ActionFilterAttribute
{
private Stream stream;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
stream = filterContext.HttpContext.Response.Filter;
stream = new MemoryStream();
filterContext.HttpContext.Response.Filter = stream;
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
StreamReader responsereader = new StreamReader(filterContext.HttpContext.Response.Filter); //empty stream? why?
responsereader.BaseStream.Position = 0;
string response = responsereader.ReadToEnd();
ContentResult contres = new ContentResult();
contres.Content = response;
filterContext.Result = contres;
}
}
我已经确定 StreamReader(stream).ReadToEnd() 返回一个空字符串,但我不明白为什么。
有什么想法如何解决这个问题吗?
编辑:我已将 OnActionExecuted 更改为 OnResultExecuted,现在在生成视图后调用它,但流仍然是空的!
I've got the following filter in place on an action to capture the HTML output, convert it to a string, do some operations to modify the string, and return a ContentResult with the new string. Unfortunately, I keep ending up with an empty string.
private class UpdateFilter : ActionFilterAttribute
{
private Stream stream;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
stream = filterContext.HttpContext.Response.Filter;
stream = new MemoryStream();
filterContext.HttpContext.Response.Filter = stream;
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
StreamReader responsereader = new StreamReader(filterContext.HttpContext.Response.Filter); //empty stream? why?
responsereader.BaseStream.Position = 0;
string response = responsereader.ReadToEnd();
ContentResult contres = new ContentResult();
contres.Content = response;
filterContext.Result = contres;
}
}
I've pinned down that StreamReader(stream).ReadToEnd() returns an empty string, but I can't figure out why.
Any ideas how to fix this?
EDIT: I've changed the OnActionExecuted to OnResultExecuted, and now it is called after the View has been generated, but the stream is still empty!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我通过劫持 HttpWriter 并将其写入 StringBuilder 而不是响应中来解决这个问题,然后在将其写入输出之前对响应执行任何需要执行的操作。
上面的代码使用 HttpContext 来避免线程错误 - 请参阅 jaminto 的评论
I solved this by hijacking the HttpWriter, and having it write into a
StringBuilder
rather than the response, and then doing whatever needs to be done to/with the response before writing it to the output.Above code using the HttpContext to avoid threading errors - see jaminto's comment
在阅读流之前,尝试通过设置
Position = 0;
将流倒回到开头。Try rewinding the stream to the beginning by setting
Position = 0;
before you read it.我想我已经开发了一个很好的方法来做到这一点。
OnClose
方法并根据需要播放流。然后,您可以从此派生到另一个属性来访问流并获取 HTML:
I think I've developed a pretty good way to do this.
OnClose
method and play with the stream as you like.You can then derive from this to another attribute to access the stream and get the HTML:
您可以验证 OnActionExectulated 方法中的流不为 NULL 吗?我不确定流变量的状态是否通过该过程存储。
为什么不尝试将流从filterContext中取出:
Can you verify that stream is not NULL in the OnActionExectuted-method? I'm not sure the state of the stream-variable is being stored through the process..
Why don't you try to get the stream out of the filterContext: