Hotmod - HTML Output Modification
This post introduces the motivation behind the Hotmod project.
Using ASP.NET to generate HTML, either with webforms or MVC, can leave the resulting output looking somewhat messy. The indentation is inconsistent and can be harder to debug when a viewing a page's source.
Inline HTML generation using server-side controls or helpers is great on an ad-hoc basis, but sometimes this gets very un-DRY. For example, say you want to enter application relative URLs in your HTML and have them expanded at runtime to their equivalent full paths. This is essential when your web application is going to run inside a virtual directory, not at the root.
In ASP.NET MVC you'll probably write something like:
<a href="<%: Url.Content("~/content/foo.html") %>">foo</a>
That works, but I think the intent could be clearer.
Why can't we just type:
and establish a convention that all URLs in the HTML that start with a "~" need to be expanded into their absolute form?
What we need is a post-processor for HTML generated by ASP.NET.
Hotmod is exactly that.
ASP.NET is very extensible, Hotmod contains an implementation of IHttpModule that hooks into the web request pipeline to provide some funky post-processing for your HTML output.
Hotmod will capture the HTML generated by your web form pages and MVC views, parse the document into an XDocument and then apply any number of modifiers. The resulting HTML can be either "pretty printed" with nice indentation, or minified to remove all unnecessary whitespace.
Your HTML will need to be parseable as XML for this work. So I hope you are closing your tags correctly and using quotes around attribute values!
Hotmod defines a simple IModifier interface you can implement to add any application-wide modifications you want to make to the HTML. For example, you could add a generation timestamp footer in a comment.
The approach let's you be much more compositional than trying to nest multiple master pages.
Hotmod is an open source project. Get the code from Hotmod on Github.