-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Closed
Labels
Description
Might be an issue ... or more likely ... my noob 👶 status with MVC. I can post on SO if this definitely not an "issue."
I'm setting up an ActionFilterAttribute
to do some OnActionExecuted
post processing of content. I can write to the HttpContext.Response.Body
with a StreamWriter
... that works just fine. I'm trying to get the content so I can work with it before writing it out, but attempts to use HttpContext.Response.Body
constantly give me Stream was not readable
. How do I read Response.Body
here, or is the fact that I can't read the stream an issue?
This works fine and will shoot the text out as the output of the View() when the ActionResult
has the filter annotation:
public class PostProcessFilterAttribute : ActionFilterAttribute {
public override void OnActionExecuted(ActionExecutedContext filterContext) {
var content = filterContext.HttpContext.Response;
using (var responseWriter = new StreamWriter(content.Body, Encoding.UTF8)) {
responseWriter.Write("This is some text to write!");
}
base.OnActionExecuted(filterContext);
}
}
This attempt to read the Response.Body
chokes:
public class PostProcessFilterAttribute : ActionFilterAttribute {
public override void OnActionExecuted(ActionExecutedContext filterContext) {
var content = filterContext.HttpContext.Response;
// Next line throws "Stream was not readable"
using (var responseReader = new StreamReader(content.Body)) {
// I'd like to do some work with the stream here before shooting it back out
using (var responseWriter = new StreamWriter(content.Body, Encoding.UTF8)) {
responseWriter.Write(responseReader.ReadToEnd());
}
}
base.OnActionExecuted(filterContext);
}
}