I have a few Razor pages with lots of conditional logic, loops, partial views etc. It’s easy to keep the output markup semantically correct, but more difficult to format it with the right indentation and line breaks. How could I do this automatically at runtime? Is there a module or Razor extension?
Please don’t say Right click -> Format selection. To be clear, I want to avoid having to think about formatting when I write the Razor pages. I want my Razor markup to be laid out in a way that makes sense to developers (indenting inside server-side blocks, for example) – but the rendered HTML to be ‘prettified’ for the odd person who clicks ‘View source’. (I’m not worried about increasing the size of the output because I’m using gzip/deflate.)
You can use a library like
TidyNet( http://sourceforge.net/projects/tidynet/ ), an implement anActionFilter:The algorithm of the filter:
And then you just plug it into the Controller:
Voilá! 😉
I learned it from this source: http://blog.aquabirdconsulting.com/2009/10/28/asp-net-mvc-clean-html/