Ban HTML comments from your pages and views
Posted
by Bertrand Le Roy
on ASP.net Weblogs
See other posts from ASP.net Weblogs
or by Bertrand Le Roy
Published on Fri, 02 Apr 2010 21:39:31 GMT
Indexed on
2010/04/02
21:43 UTC
Read the original article
Hit count: 576
Too many people don’t realize that there are other options than <!-- --> comments to annotate HTML. These comments are harmful because they are sent to the client and thus make your page heavier than it needs to be.
When doing ASP.NET, a simple drop-in replacement is server comments, which are delimited by <%-- --%> instead of <!-- -->. Those server comments are visible in your source code, but will never be rendered to the client.
Here’s a simple way to sanitize a web site. From Visual Studio, hit CTRL+H to bring the search and replace dialog.
Choose “Replace in Files” from the second meny on top of the dialog. Open the find options, check “use” and make sure “Regular expressions” are selected. Use “*.aspx;*.ascx;” as the file types to examine. Choose “Entire Solution” under “Look in”.
Here’s the expression to search for comments:
\<!--{[^-]*}--\>
And here’s the replacement string:
<%--\1--%>
I usually use the “Find Next” and “Replace” buttons rather than the more brutal “Replace All” in order to not apply the fix blindingly. Once this is done, I do a second manual pass of finds with the same expression to make sure I didn’t miss anything.
© ASP.net Weblogs or respective owner