You can capture output recevier before the View gets rendered by overriding Render method of the WebFormView class. The trick is that the output receiver is not the System.IO.TextWriter writer but the Writer property of the viewContext.
Also, you have to extend WebFormViewEngine to return your views.
public class MyViewEngine : WebFormViewEngine
{
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
return new MyView(partialPath, null);
}
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
return new MyView(viewPath, masterPath);
}
}
public class MyView : WebFormView
{
public MyView(string inViewPath, string inMasterPath) : base(inViewPath, inMasterPath) { }
public MyView(string inViewPath) : base(inViewPath) { }
public override void Render(ViewContext viewContext, System.IO.TextWriter writer)
{
//make a switch to custom output receiver
var oldWriter = viewContext.Writer;
viewContext.Writer = new System.IO.StringWriter();
base.Render(viewContext, null);
viewContext.Writer.Close();
//get output html
var html = ((System.IO.StringWriter)viewContext.Writer).GetStringBuilder();
//perform processing
html.Replace('a', 'b');
//retransmit output
viewContext.Writer = oldWriter;
viewContext.Writer.Write(html);
}
}