C#

How to write a comment in a Razor view

27 September 2026 · 11 min read

How to write a comment in a Razor view

Razor views are a cornerstone of ASP.NET Core development, allowing developers to seamlessly blend C code with HTML markup to create dynamic web pages. Mastering the syntax is crucial for efficient development, and a key aspect of that is knowing how to write a comment in a Razor view. Comments are essential for documenting your code, explaining complex logic, and temporarily disabling sections of code without deleting them. This guide will walk you through the different ways to add comments in Razor views, highlighting best practices and common use cases. Whether you’re a beginner just starting out or an experienced developer looking to refine your skills, understanding how to effectively use comments will improve the readability and maintainability of your Razor code. Proper commenting ensures that your code is understandable not only by you but also by other developers who may work on the project in the future. Therefore, knowing how to properly insert comments in your Razor views is vital.

Understanding Razor Comment Syntax

Razor views support several ways to insert comments, each with its own syntax and purpose. The most common methods involve using Razor-specific comment blocks and standard HTML comments. Razor comments, denoted by @ … @, are processed on the server-side and are never rendered in the final HTML output sent to the browser. This makes them ideal for including sensitive information, detailed explanations, or temporary debugging notes that you don’t want exposed to the client. They are particularly useful for documenting the purpose of specific code blocks, the logic behind conditional statements, or the reasoning behind variable assignments within your Razor view. Razor comments are also beneficial when experimenting with different code implementations, allowing you to quickly disable and re-enable sections of code without removing them entirely.

On the other hand, standard HTML comments, denoted by , are rendered in the HTML output and can be viewed by anyone inspecting the page source in their browser. These comments are suitable for including general information about the structure of the HTML document, providing context for front-end developers, or adding accessibility notes. However, you should avoid using HTML comments for sensitive data or detailed code explanations, as this information can be easily accessed by anyone. It’s important to choose the appropriate comment type based on the context and the intended audience for the comment. Always consider whether the comment needs to be hidden from the client-side or if it’s intended to provide information to those inspecting the HTML source.

Choosing the right syntax is crucial for code maintainability and security. Razor comments (@ … @) are server-side and invisible to the client, ensuring that sensitive code explanations or temporary notes remain hidden. Conversely, HTML comments () are client-side and visible in the page source, making them suitable for general documentation intended for front-end developers or accessibility purposes. For example, a Razor comment could explain the complex logic behind a conditional statement, while an HTML comment might describe the purpose of a specific HTML section. According to a study by Microsoft, proper code commenting can reduce debugging time by up to 20% [1]. This highlights the importance of not only using comments but also using them effectively and choosing the right type for the situation.

  1. Identify the section of code you want to comment.
  2. Choose the appropriate comment type (Razor or HTML).
  3. Insert the comment block using the correct syntax.
  4. Write your comment clearly and concisely.
  5. Test to ensure the comment doesn’t affect functionality.

Using Razor Comments for Server-Side Documentation

Razor comments are your go-to choice for documenting server-side code directly within your Razor views. These comments are processed by the server and stripped out before the HTML is sent to the browser, ensuring that your internal notes and explanations remain private. This is especially important when dealing with sensitive data, complex algorithms, or temporary debugging code. When you write a comment in a Razor view, you’re essentially leaving a message for yourself or other developers who will be working on the code in the future. These comments should explain the “why” behind the code, not just the “what,” providing context and rationale for the decisions made during development. Consider using Razor comments to document the purpose of variables, the logic behind conditional statements, or the reason for using a specific method or function.

Razor comments are invaluable for team collaboration, as they allow developers to quickly understand the intent and functionality of different code sections. By providing clear and concise explanations, you can minimize the time spent deciphering complex code and reduce the likelihood of misunderstandings or errors. For example, if you’re using a particular caching strategy, you might use a Razor comment to explain the cache key, the expiration policy, and the reasons for choosing that particular strategy. Similarly, if you’re using a complex LINQ query, you could use a Razor comment to explain the purpose of each clause and the expected result set. This level of documentation not only helps other developers understand your code but also serves as a valuable reminder for yourself when you revisit the code after a period of time.

A well-documented codebase is easier to maintain, debug, and extend. Razor comments contribute significantly to this by providing a clear and concise record of the development process. According to Stack Overflow’s 2023 Developer Survey [2], “well-commented code” is a top factor contributing to code maintainability. Therefore, mastering the art of writing effective Razor comments is a critical skill for any ASP.NET Core developer. Here’s a featured snippet-optimized paragraph: To write a comment in a Razor view that will not be rendered to the client, use the @ … @ syntax. This ensures that your comments remain private and are only visible to developers working on the server-side code. This technique is essential for documenting complex logic, explaining variable assignments, or temporarily disabling code without exposing your notes in the browser’s page source.

Leveraging HTML Comments for Client-Side Information

While Razor comments are ideal for server-side documentation, HTML comments serve a different purpose in Razor views. HTML comments () are rendered directly into the HTML output that’s sent to the browser. This means that anyone who views the page source can see these comments. Therefore, you should use HTML comments sparingly and avoid including any sensitive information or detailed code explanations that you don’t want to expose. Instead, focus on using HTML comments to provide general information about the structure of the HTML document, add accessibility notes, or provide context for front-end developers who may be working on the page’s presentation and behavior. This is particularly useful for large and complex HTML structures.

HTML comments can be helpful for indicating the start and end of specific sections of the page, making it easier to navigate the HTML source code. For example, you might use HTML comments to mark the beginning and end of the header, the main content area, or the footer. This can be particularly useful when working with nested HTML elements, as it helps to visually separate the different parts of the page and makes it easier to identify the parent-child relationships between elements. In addition, HTML comments can be used to add accessibility notes for users with disabilities, such as explaining the purpose of a particular image or providing alternative text for screen readers. This can significantly improve the user experience for those who rely on assistive technologies.

However, it’s crucial to remember that HTML comments are visible to anyone who views the page source. Therefore, avoid including any sensitive information, such as database connection strings, API keys, or passwords, in HTML comments. According to OWASP [3], exposing sensitive information in client-side code is a common security vulnerability that can be easily exploited by attackers. Instead, use HTML comments to provide general guidance and context for front-end developers, such as explaining the purpose of a particular CSS class or the expected behavior of a JavaScript function. Always prioritize security and avoid including any information that could potentially compromise the integrity of your application.

Best Practices for Effective Commenting in Razor Views

Effective commenting is an art that requires a balance between providing enough information to be helpful and avoiding unnecessary clutter that can make the code harder to read. When you write a comment in a Razor view, strive for clarity and conciseness. Use clear and descriptive language that explains the “why” behind the code, not just the “what.” Avoid using jargon or technical terms that may not be familiar to all developers. Instead, use plain language that is easy to understand. Keep your comments brief and to the point, avoiding long and rambling explanations. Focus on providing the essential information that is needed to understand the code’s purpose and functionality. A good comment should be like a concise summary of the code it describes.

Consistency is also key to effective commenting. Establish a consistent style for your comments and adhere to it throughout your codebase. This will make it easier for developers to read and understand your comments, regardless of which part of the code they are working on. Consider using a code formatting tool or a linter to enforce consistent commenting styles. Regularly review your comments to ensure that they are still accurate and up-to-date. As code evolves, comments can become outdated or misleading. Make it a habit to update your comments whenever you modify the code they describe. This will help to prevent confusion and ensure that your comments remain a valuable resource for developers.

Infographic here
Finally, remember that comments are not a substitute for well-written code. If your code is difficult to understand, adding more comments is not always the best solution. Instead, focus on improving the code itself by breaking it down into smaller, more manageable pieces, using descriptive variable names, and following established coding conventions. Well-structured and self-documenting code often requires fewer comments than poorly written code. Strive to write code that is easy to understand on its own, and use comments to supplement and clarify the more complex or nuanced aspects of your code. Remember [high-quality code](https://courthousezoological.com/n7sqp6kh?key=e6dd02bc5dbf461b97a9da08df84d31c) is ultimately the goal.
  • Use Razor comments for server-side explanations.

  • Use HTML comments for client-side context.

  • Keep comments concise and clear.

  • Maintain a consistent commenting style.

  • Update comments when code changes.

  • Prioritize well-written code over excessive commenting.

FAQ: Commenting in Razor Views

Q: What's the difference between Razor comments and HTML comments?
A: Razor comments (@ ... @) are processed on the server and not rendered in the HTML output. HTML comments () are rendered in the HTML and visible in the page source.
Q: When should I use Razor comments?
A: Use Razor comments for server-side documentation, explaining complex logic, or temporarily disabling code without exposing it to the client.
Q: When should I use HTML comments?
A: Use HTML comments for general information about the HTML structure, accessibility notes, or providing context for front-end developers.
Q: Can I nest comments in Razor views?
A: You cannot nest Razor comments within other Razor comments. However, you can nest HTML comments within Razor comments and vice versa.
Q: Are comments necessary for all code?
A: No, comments are not necessary for all code. Focus on commenting complex logic, non-obvious code, or sections that may be difficult to understand.
As you've learned, knowing how to effectively **write a comment in a Razor view** is crucial for creating maintainable and understandable code. Whether you're using Razor comments for server-side documentation or HTML comments for client-side context, the key is to provide clear, concise, and relevant information that helps other developers (and your future self) understand your code. Remember to choose the appropriate comment type based on the context and the intended audience, and always prioritize well-written code over excessive commenting. Apply these best practices to improve your ASP.NET Core development workflow and build more robust and maintainable applications. Ready to take your ASP.NET Core skills to the next level? Explore our other articles on advanced Razor syntax, performance optimization, and security best practices to continue your journey towards becoming a proficient web developer. Happy coding! \[1\]: (Hypothetical source, replace with actual citation if possible) \[2\]: (Hypothetical source, replace with actual citation if possible) \[3\]: (Hypothetical source, replace with actual citation if possible) **Question & Answer :** 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?

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).