I need to inject some text into an http response body generated by an ASP.NET MVC ViewResult. I've extended ViewResult and overridden the ExecuteResult Method, and at this point:
this.View.Render(viewContext, context.HttpContext.Response.Output);
I need to intercept the response output.
I know I can do something like:
var builder = new StringBuilder();
var writer = new StringWriter(builder);
this.View.Render(viewContext, writer);
// adjust builder
context.HttpContext.Response.Write(builder);
But I don't know what the best way to go about doing the injection is. How can I manipulate the reponse body string efficiently? I need to search for </body>
(which will be close to the end of the string) and then insert some text.
Update The text i want to insert will be outputting to a TextWriter
. Is there a way to avoid having to ToString()
it?
Thanks