Dev*_*nia 1 asp.net-mvc razor asp.net-mvc-3
我喜欢这个新的视图引擎,其中一个最酷的功能是默认编码所有内容.但有一些我不明白的东西,让我们说我已经创建了这个HTML段落
<p>
this is a test Paragraph for both Development & Production
</p>
Run Code Online (Sandbox Code Playgroud)
现在,如果我检查萤火虫中的元素我得到
<p>
this is a test Paragraph for both Development & Production
</p>
Run Code Online (Sandbox Code Playgroud)
这是正确的,但如果我在浏览器中查看源代码,我得到
<p>
this is a test Paragraph for both Development & Production
</p>
Run Code Online (Sandbox Code Playgroud)
并尝试使用W3C验证验证页面我得到"&"字符的错误无效.
现在我的文件正如我认为的那样正确
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11 /DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
Aar*_*ker 11
默认情况下,Razor将编码它所写的所有内容,而不是页面中的所有内容.由于Razor事物P标签内的文本是HTML,它不会对它进行任何编码.
请查看以下示例,以便更好地了解差异:
<p>this is a test Paragraph for both Development & Production</p>
@{var body = "this is a test Paragraph for both Development & Production";}
<p>@body</p>
@{var body2 = new HtmlString("this is a test Paragraph for both Development & Production");}
<p>@body2</p>
Run Code Online (Sandbox Code Playgroud)
如果你渲染这个页面你会注意到第一段没有被编码,而第二段将被编码,因为它编码变量的输出.然后第三个例子将产生与第一个相同的输出,因为它使用特殊的HtmlString,假设输入是安全的.
作为参考,这是Razor的输出:
<p>this is a test Paragraph for both Development & Production</p>
<p>this is a test Paragraph for both Development & Production</p>
<p>this is a test Paragraph for both Development & Production</p>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2107 次 |
| 最近记录: |