Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a comment in a Razor view?

How to write a comment in a MVC view, that won't be transmitted to the final HTML (i.e.,to browser, to response). One can make a comment with:

<!--<a href="/">My comment</a> -->

but, it is visible in the page source code in browser.

Is it possible to leave comments in '.cshtml' files only for internal use?

like image 226
horgh Avatar asked Oct 15 '22 02:10

horgh


People also ask

How do you comment on Razor view?

Razor View Engine has two types of comments, one is single-line and another is multiline. Razor uses the syntax "@* .. *@" for the comment block but in a C# code block we can also use "/* */" or "//".

How do you write a comment on a view?

Note that in general, IDE's like Visual Studio will markup a comment in the context of the current language, by selecting the text you wish to turn into a comment, and then using the Ctrl + K Ctrl + C shortcut, or if you are using Resharper / Intelli-J style shortcuts, then Ctrl + / .

Is used to add comment in Razor syntax?

In C#, we use (“//") for commenting, but in Razor, we will define comments like ("@* *@").


1 Answers

Note that in general, IDE's like Visual Studio will markup a comment in the context of the current language, by selecting the text you wish to turn into a comment, and then using the Ctrl+K Ctrl+C shortcut, or if you are using Resharper / Intelli-J style shortcuts, then Ctrl+/.

Server side Comments:

Razor .cshtml

Like so:

@* Comment goes here *@

.aspx
For those looking for the older .aspx view (and Asp.Net WebForms) server side comment syntax:

<%-- Comment goes here --%>

Client Side Comments

HTML Comment

<!-- Comment goes here -->

Javascript Comment

// One line Comment goes Here
/* Multiline comment
   goes here */

As OP mentions, although not displayed on the browser, client side comments will still be generated for the page / script file on the server and downloaded by the page over HTTP, which unless removed (e.g. minification), will waste I/O, and, since the comment can be viewed by the user by viewing the page source or intercepting the traffic with the browser's Dev Tools or a tool like Fiddler or Wireshark, can also pose a security risk, hence the preference to use server side comments on server generated code (like MVC views or .aspx pages).

like image 310
StuartLC Avatar answered Oct 16 '22 14:10

StuartLC