如何在 Razor 视图中写评论? [英] How to write a comment in a Razor view?

查看:24
本文介绍了如何在 Razor 视图中写评论?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

推荐答案

注意,一般来说,IDE 就像 Visual Studio 一样,会在当前语言的上下文中标记注释,通过选择要转换的文本注释,然后使用 Ctrl+K Ctrl+C 快捷方式,或者如果您使用的是 Resharper/Intelli-J 风格的快捷键,然后 Ctrl+/.

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+/.

服务器端评论:

Razor .cshtml

像这样:

@* Comment goes here *@

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

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

<%-- Comment goes here --%>

客户端评论

HTML 注释

<!-- Comment goes here -->

Javascript 注释

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

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

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

这篇关于如何在 Razor 视图中写评论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆