27 Apr

ASP.NET: How to correctly comment markup code

I am working with an ASP.NET MVC Application and had problems with some of my calls to the HtmlHelper Extension functions. The following code
Original call to HTML Helper

Original call to HTML Helper

produced the following exception

HTML Helper Exception

HTML Helper Exception

In order to analyze the problem I started with commenting the codeline in question like this

Wrong way to comment code in ASP.NET

Wrong way to comment code in ASP.NET

Well – unfortunately that is not the way to comment source code in your markup file. This markup code gets compiled into the temporary .NET Assembly like this

Generated Code based on Markup Code

Generated Code based on Markup Code

Even though the output of my HtmlHelper call will be under comments in the final HTML – the method is still called – causing the same exception to happen.

To do this right – you have to comment the code within the code block like this

Correct way to comment code in ASP.NET Markup

Correct way to comment code in ASP.NET Markup

Conclusion

Makes sense? Yes – it does. But if you don’t know it you may be surprised about the result.

Comments
29 comments yet
  1. [...] to VoteASP.NET: How to correctly comment markup code (4/27/2009)Monday, April 27, 2009 from Andreas GrabnerI am working with an ASP.NET MVC Application and had [...]

  2. Performance, Scalability and Architecture – Java and .NET Application Performance Management (dynaTrace Blog) » ASP.NET: How to correctly comment markup code…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

  3. What’s wrong with server-side comments?

  4. ≶%– This is commented-out –>

  5. <%– This is commented-out –%>

    Damn those unescaped blog comments…

  6. @cw: Thanks for that
    You are right – the “proper” way would be to use server-side comments.
    There is a nice blog-entry worth checking out: http://weblogs.asp.net/scottgu/archive/2006/07/09/Tip_2F00_Trick_3A00_-Using-Server-Side-Comments-with-ASP.NET-2.0-.aspx

    I just wanted to make the point that – using xml style comments in your markup code doesn’t do what you may think.

  7. Thank you in advance for your quick answer !. Very nice post.

  8. Useful information like this one must be kept and maintained so I will put this one on my bookmark list! Thanks for this wonderful post and hoping to post more of this!

  9. asp.net comments using server side comments that can disabled code controls in the page.

  10. http://itrecords.blogspot.com/2009/10/best-remote-in-town.html
    Thanks for posting this type of article. Your topic really caught my attention. Please post more I know that your knowledge is not only limited here.

    Good job!

    http://www.yannysinlove.blogspot.com/

  11. Very interesting post. I’ll be spending more time with certain areas. Well done and good luck with your work. Wish I have the luxury of time to consider using the benefits these site can offer. Thanks for the share. I made a copy of the list and will check all.

  12. In Beijing August 5, news NFL jerseys reporter HuaXue JiLinTai (China) according to the sound of the news reports, at 7:09 SongYuan stand the test, but in jilin tonghua, new challenges still is imminent,cheap NFL jerseys although TongHuaShi yesterday, but recovered water coming round to the strong rainfall, the water piping erection temporary has brought serious test.

    Screw tap water flowing water, a common wholesale NFL jerseys heart melts.

  13. Pretty Nice Site. Spray Foam Insulation

  14. Pretty nice stuff.
    Grants

  15. fastfinder @ 2010-11-01 14:34

    This article creates the new innovative ideas which are being very helpful for me. It’s a perfect way for discussing about the topic, I am sure it will bring you lot of new readers. Regards, James from flash chat

  16. JosephM @ 2010-11-13 00:12

    Thanks for the “correct” way on how to do it :)
    Bear Spray

  17. Paul De Sogino @ 2010-12-24 15:28

    The better way is to use Visual Studio.NET’s multi-line comment functionality. Just the ‘Text Editor’ toolbar must be checked under Tools / Customize… / Toolbars.
    BR,
    baccarat

  18. Even though the output of my HtmlHelper call will be under comments in the final HTML – the method is still called – causing the same exception to happen.

  19. the point that – using xml style comments in your markup code doesn’t do what you may think.PO Box

  20. asp.net comments using server side comments that can disabled code controls in the page.

  21. I am working with an ASP.NET MVC Application and had problems with some of my calls to the HtmlHelper Extension functions. The following code

  22. Even though the output of my HtmlHelper call will be under comments in the final HTML – the method is still called – causing the same exception to happen.

  23. Thank you in advance for your quick answer !. Very nice post.

  24. A great post, thanks for taking the time to share, continued success to your site in the future! GOOD Work!!

  25. Thanks I have been searching for this forever.

  26. It is rare these days to find websites with useful data .I am relieved I came upon this site.
    DORIS

  27. Excellent article, very nice post!

  28. avena bailey @ 2011-12-29 08:15

    You are so talented in writing. God is really using you in tremendous methods. You are doing a great job! This was a wonderful article.Thank you
    gold price per ounce live

  29. asp.net comments using server side comments that can disabled code controls in the page.

Add your comment now

*