使用控制器操作过滤器捕获 HTML 输出

编程入门 行业动态 更新时间:2024-10-09 18:24:33
本文介绍了使用控制器操作过滤器捕获 HTML 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在一个动作上设置了以下过滤器来捕获 HTML 输出,将其转换为字符串,执行一些操作来修改字符串,并返回一个带有新字符串的 ContentResult.不幸的是,我一直以空字符串结尾.

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; } }

我确定 StreamReader(stream).ReadToEnd() 返回一个空字符串,但我不知道为什么.

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:我把OnActionExecuted改成了OnResultExecuted,现在View生成后调用了,但是流还是空的!

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!

推荐答案

我通过劫持 HttpWriter 解决了这个问题,并将其写入 StringBuilder 而不是响应,然后做任何需要的事情在将响应写入输出之前对其进行/处理.

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.

private class UpdateFilter : ActionFilterAttribute { private HtmlTextWriter tw; private StringWriter sw; private StringBuilder sb; private HttpWriter output; public override void OnActionExecuting(ActionExecutingContext filterContext) { sb = new StringBuilder(); sw = new StringWriter(sb); tw = new HtmlTextWriter(sw); output = (HttpWriter)filterContext.RequestContext.HttpContext.Response.Output; filterContext.RequestContext.HttpContext.Response.Output = tw; } public override void OnResultExecuted(ResultExecutedContext filterContext) { string response = sb.ToString(); //response processing output.Write(response); } }

使用 HttpContext 避免线程错误的上述代码 - 参见 jaminto 的评论

Above code using the HttpContext to avoid threading errors - see jaminto's comment

private class RenderFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); HtmlTextWriter tw = new HtmlTextWriter(sw); HttpWriter output = (HttpWriter)filterContext.RequestContext.HttpContext.Response.Output; filterContext.HttpContext.Items["sb"] = sb; filterContext.HttpContext.Items["output"] = output; filterContext.RequestContext.HttpContext.Response.Output = tw; } public override void OnResultExecuted(ResultExecutedContext filterContext) { string response = filterContext.HttpContext.Items["sb"].ToString(); //response processing ((HttpWriter)filterContext.HttpContext.Items["output"]).Write(response); } }

更多推荐

使用控制器操作过滤器捕获 HTML 输出

本文发布于:2023-11-17 11:11:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609825.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   控制器   操作   HTML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!