ASP.NET Server-side comments
- by nmarun
I believe a good number of you know about Server-side commenting. This blog is just like a revival to refresh your memories. When you write comments in your .aspx/.ascx files, people usually write them as: 1: <!-- This is a comment. -->
To show that it actually makes a difference for using the server-side commenting technique, I’ve started a web application project and my default.aspx page looks like this:
1: <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ServerSideComment._Default" %>
2: <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
3: </asp:Content>
4: <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
5: <h2>
6: <!-- This is a comment -->
7: Welcome to ASP.NET!
8: </h2>
9: <p>
10: To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
11: </p>
12: <p>
13: You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
14: title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
15: </p>
16: </asp:Content>
See the comment in line 6 and when I run the app, I can do a view source on the browser which shows up as:
1: <h2>
2: <!-- This is a comment -->
3: Welcome to ASP.NET!
4: </h2>
Using Fiddler shows the page size as:
Let’s change the comment style and use server-side commenting technique.
1: <h2>
2: <%-- This is a comment --%>
3: Welcome to ASP.NET!
4: </h2>
Upon rendering, the view source looks like:
1: <h2>
2:
3: Welcome to ASP.NET!
4: </h2>
Fiddler now shows the page size as:
The difference is that client-side comments are ignored by the browser, but they are still sent down the pipe. With server-side comments, the compiler ignores everything inside this block.
Visual Studio’s Text Editor toolbar also puts comments as server-side ones. If you want to give it a shot, go to your design page and press Ctrl+K, Ctrl+C on some selected text and you’ll see it commented in the server-side commenting style.