tags:

views:

110

answers:

2

In Monorail there's a transform filter concept, where a rendered view can then have further processing applied to it. Use cases:

  • Process markdown
  • Remove whitespace
  • Strip unwanted characters

I suspect there is no out-of-the-box method of doing this in ASP.NET MVC but does anyone have a suggested approach? I'm using the NVelocity view engine.

+1  A: 

Create a custom ActionFilter and override the OnResultExecuted method to do your extra processing then decorate your controller with that attribute.

kazimanzurrashid
How do you get the rendered view in the OnResultExecuted though?
A: 

A possible solution which I've implemented is to create a custom ViewResult and do the work in there. This is not an elegant solution, as I've basically just copied and pasted the normal implementation into the overridden ExecuteResult and tweaked the rendered output there. This line:

View.Render(viewContext, context.HttpContext.Response.Output);

Becomes:

TextWriter writer = new StringWriter();

View.Render(viewContext, writer);

string renderedResult = writer.ToString();

renderedResult = renderedResult.Replace("hello", "goodbye");

context.HttpContext.Response.Output.Write(renderedResult);

The relevant source is here: http://aspnet.codeplex.com/SourceControl/changeset/view/23011#288022

This is a pretty horrible way of doing it IMO, but it works.