Quantcast

ASP.NET: Remove HTML Comments at Runtime

Get the WebProNews Newsletter:
[ Business]

I’ve been playing a bit with regular expressions lately and have to find some useful tasks in order to practice the skill.

So, today I wanted to make a little method that strips HTML comments from an ASP.NET webpage at runtime. The practical use of the exercise is somewhat limited for most developers, but some websites have so many comments that it might just save a decent amount of bytes from the response stream.

The problem with this exercise is that a lot of JavaScript is using HTML comments to hide it’s workings from older browsers. That would mean that those script tags would be empty. That’s why I made a rule saying that every JavaScript has to implement the HTML comments correctly. Some don’t so you have to change it your self.

This is how the JavaScript is wrongly commented which also breaks my regex.

<script type="text/javascript">
<!--
   function Name()
   {
   }
-->
</script>

The commenting should look like this which is also the right way to do it.

<script type="text/javascript">
<!--//
   function Name()
   {
   }
//-->
</script>

The regular expression is very simple and all you need to do is to add the following method to your webpage, user control or master page.

using System.IO;
using System.Text;
using System.Text.RegularExpressions;

   private static Regex _Regex = new Regex("((<!-- )((?!<!-- ).)*( -->))(\r\n)*", RegexOptions.Singleline);

   protected override void Render(HtmlTextWriter writer)
{
   using (StringWriter sw = new StringWriter())
   {
     using (HtmlTextWriter htmlWriter = new HtmlTextWriter(sw))
     {
       base.Render(htmlWriter);
       writer.Write(_Regex.Replace(sw.ToString(), string.Empty));
     }
   }
}

Maybe not the most useful stuff I’ve ever written, but fine for learning. The only thing that bugs me is the JavaScript rule.

Comments

Tag:

Add to Del.icio.us | Digg | Reddit | Furl

Bookmark WebProNews:

Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

ASP.NET: Remove HTML Comments at Runtime
About Mads Kristensen
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/ WebProNews Writer
Top Rated White Papers and Resources
  • http://www.webpronews.com/comment/reply/33631 Guest