As part of the development of Momentum, RedStar's new and improved CMS I had to inject 

html directly into every page that is rendered by the CMS. I have done this is the past using either an inherited version of System.Web.UI.Page or using an IHttpModule, but System.Web.UI.

Page simply isn't a part of MVC and the IHttpModule is 

troublesome to say the least.

After doing some research I managed to find a solution, which is to is an ActionFilterAttribute which can then be registered through the [global.

asax](http://en.wikipedia.org/wiki/Global.asax) file. I have included the basic code below.

Create a class with the following:

public class MyActionFilterAttribute : ActionFilterAttribute  
{   
private HtmlTextWriter tw;   
private StringWriter sw;   
private StringBuilder sb;   
private HttpWriter output;    
public override void  OnActionExecuting(ActionExecutingContex 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();    
//Update response.    
output.Write(response);    
}   
}

Then add the following to your global.asax file:

protected void Application_Start()  
{    
AreaRegistration.RegisterAllAreas();    
GlobalFilters.Filters.Add(new MyActionFilterAttribute());   
RegisterGlobalFilters(GlobalFilters.Filters);    
RegisterRoutes(RouteTable.Routes);    
}