131

What is the syntax for server side comment in razor view?

I want to comment this code:

/*
@helper NavItem() {

}
*/
Muntasir
  • 798
  • 1
  • 14
  • 24
stacker
  • 14,641
  • 17
  • 46
  • 74

4 Answers4

233
@* here is the code to comment *@
JarrettV
  • 18,845
  • 14
  • 46
  • 43
  • 37
    In visual studio, select some code/markup in your razor view and press Ctrl+K, Ctrl+C, and it'll comment the selection as described above. – MrBoJangles Feb 17 '11 at 18:12
50

Both of the following work

@{
/*
    This is a comment
*/}


@//This is another comment

Update

With the new Beta of MVC 3 out the old methods of highlighting won't work.

@{
    //This is a comment
}

@{/*
      This is a multi
      line comment
*/}

@*
      This is a comment, as well
*@

Is the updated method @//This is a comment and @/* */ will no longer work.

Buildstarted
  • 26,529
  • 10
  • 84
  • 95
  • I didn't downvote, but this answer is *wrong* as of the RC. @JarrettV has the correct way to do server-side comments. – TheCloudlessSky Nov 15 '10 at 13:19
  • 2
    Actually, it's still correct but they added a new `@* *` syntax. So now there are three ways to comment. Not just the one by JarretV. – Buildstarted Nov 15 '10 at 17:26
  • @BuildStarted - True - perhaps I was a little harsh :). – TheCloudlessSky Nov 16 '10 at 02:33
  • Heh, it's a problem with text based communication - I didn't view your response as harsh - I just thought maybe there was a problem with how microsoft said comments work so I wanted to clarify. :) The @* *@ comments aren't parsed whereas @{//} and @{/* */} comments still are - that's the difference between them. – Buildstarted Nov 16 '10 at 02:53
  • 1
    @BuildStarted - Most likely pressure from this post: http://weblogs.asp.net/scottgu/archive/2010/11/12/asp-net-mvc-3-server-side-comments-with-razor.aspx#7643364 – TheCloudlessSky Nov 16 '10 at 21:15
  • Yeah, you can switch them at any time. Or just leave a question with no accepted answer. I review my questions from time to time and switch the accepted answer if there's one that seems better. – MrBoJangles Feb 16 '11 at 22:48
  • `@{/* */}` was the only one I could get to work from within a ` – Ouroborus Oct 11 '16 at 23:38
  • This is the best answer. It covers all the ways also allowed me to comment code with inner comments like: @{ @**@ }, which was not mentioned anywhere else here. – JRodd Jul 26 '17 at 17:20
9

Inside the .cshtml file, just press cntrl+k and cntrl+c, You will see the comment is automatically added by visual studio.(alternatively, cntrl_k and cntrl+u for uncommenting.) Or else if you want to write it manually then, just gohead with

@* Your Code *@
1

If its in your view, couldn't you use the standard HTML <!-- ... //--> or the .NET style <%-- .. --%>?

Jonathan Bates
  • 1,835
  • 14
  • 22
  • 3
    `<%-- --%>` will still output to the client btw – Buildstarted Aug 01 '10 at 20:34
  • 1
    The contents don't, but I have found that the whitespace is still reserved in the output. – Jonathan Bates Aug 01 '10 at 23:25
  • 2
    Well, when processed by the Razor view engine it's output just like any other "html" element. So you won't see the content because it's not rendered by the browser. But it's still output in full. (based on my experience with razor and just tested it really quick) – Buildstarted Aug 02 '10 at 04:27
  • 1
    I just tested this (with Preview 1), ASPX comments `<%-- --%>` _seem_ to work, even though this is the Razor engine, not the WebForms/ASPX engine. However, more testing reveals that Razor ignores anything within _any_ ASP tags `<% %>` (but conserves whitespace?). Code within standard HTML comments `` is still run and output, only the client will ignore its contents. – Lucas Sep 09 '10 at 15:47
  • be careful using HTML comments to comment-out view content since the razor engine will still evaluate it and if it does not compile you will get an error -- better to use the razor-specific comment delimiters – Christopher King Oct 25 '13 at 17:26