C# 如何在 Razor 视图中写评论?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12171576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:20:12  来源:igfitidea点击:

How to write a comment in a Razor view?

c#asp.net-mvc-3razorcomments

提问by horgh

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:

如何在 MVC 视图中编写评论,该评论不会传输到最终的 HTML(即,浏览器,响应)。可以发表评论:

<!--<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?

是否可以在“.cshtml”文件中留下评论仅供内部使用?

采纳答案by StuartLC

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+KCtrl+Cshortcut, or if you are using Resharper / Intelli-J style shortcuts, then Ctrl+/.

请注意,在一般情况下,IDE就像Visual Studio将标记注释当前语言的背景下,选择你所要变成注释的文本,然后使用Ctrl+ KCtrl+C快捷键,或者如果您使用ReSharper的/的Intelli J 样式快捷方式,然后Ctrl+ /

Server side Comments:

服务器端评论:

Razor .cshtml

剃刀.cshtml

Like so:

像这样:

@* Comment goes here *@

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

.aspx
对于那些寻找旧.aspx视图(和 Asp.Net WebForms)服务器端注释语法的人

<%-- Comment goes here --%>

Client Side Comments

客户端评论

HTML Comment

HTML 注释

<!-- Comment goes here -->

Javascript Comment

Javascript 注释

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

As OP mentions, although not displayed on the browser, client side comments will still be generatedfor 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).

正如 OP 所提到的,虽然没有显示在浏览器上,但仍会为服务器上的页面/脚本文件生成客户端注释,并由页面通过 HTTP 下载,除非删除(例如缩小),否则会浪费 I/O,并且,由于用户可以通过查看页面源或使用浏览器的 Dev Tools 或 Fiddler 或 Wireshark 之类的工具拦截流量来查看评论,因此也会带来安全风险,因此优先使用服务器端生成的评论代码(如 MVC 视图或 .aspx 页面)。

回答by sobering

This comment syntax should work for you:

此注释语法应该适合您:

@* enter comments here *@