在 Razor 视图中发出未编码的字符串 [英] Emitting unencoded strings in a Razor view

查看:19
本文介绍了在 Razor 视图中发出未编码的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如 ScottGu 在他的博客中所说的post «默认情况下,使用 @ 块发出的内容会自动进行 HTML 编码,以更好地抵御 XSS 攻击场景».我的问题是:如何输出非 HTML 编码的字符串?

As ScottGu says in his blog post «by default content emitted using a @ block is automatically HTML encoded to better protect against XSS attack scenarios». My question is: how can you output a non-HTML-encoded string?

为了简单起见,请坚持这个简单的案例:

For the sake of simplicity, pls stick to this simple case:

@{
 var html = "<a href='#'>Click me</a>"
 // I want to emit the previous string as pure HTML code...
}

推荐答案

这是我最喜欢的方法:

@Html.Raw("<p>my paragraph text</p>")

来源是 Phil Haack 的 Razor 语法参考:http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx

Source was Phil Haack's Razor syntax reference: http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx

这篇关于在 Razor 视图中发出未编码的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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