在 Razor 视图中发出未编码的字符串

发布于 2024-09-12 01:19:37 字数 371 浏览 1 评论 0原文

正如 ScottGu 在他的博客帖子中所说的那样 «by使用@块发出的默认内容会自动进行HTML编码,以更好地防止XSS攻击场景»。 我的问题是:如何输出非 HTML 编码的字符串?

为了简单起见,请坚持这个简单的情况:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夢归不見 2024-09-19 01:19:37

这是我最喜欢的方法:

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

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

This is my favorite approach:

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

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

原谅我要高飞 2024-09-19 01:19:37

您可以创建一个不会进行 HTML 编码的 MvcHtmlString 的新实例。

@{
  var html = MvcHtmlString.Create("<a href='#'>Click me</a>")
}

希望 Razor 的未来能有一种更简单的方法。

如果您没有使用 MVC,您可以尝试以下操作:

@{
  var html = new HtmlString("<a href='#'>Click me</a>")
}

You can create a new instance of MvcHtmlString which won't get HTML encoded.

@{
  var html = MvcHtmlString.Create("<a href='#'>Click me</a>")
}

Hopefully there will be an easier way in the future of Razor.

If you're not using MVC, you can try this:

@{
  var html = new HtmlString("<a href='#'>Click me</a>")
}
征棹 2024-09-19 01:19:37

new HtmlString 绝对是答案。

我们研究了其他一些 razor 语法更改,但最终没有一个真正比新的 HtmlString 更短。

然而,我们可以将其包装到一个助手中。可能...

@Html.Literal("<p>something</p>")

或者

@"<p>something</p>".AsHtml()

new HtmlString is definitely the answer.

We looked into some other razor syntax changes, but ultimately none of them ended up really being any shorter than new HtmlString.

We may, however, wrap that up into a helper. Possibly...

@Html.Literal("<p>something</p>")

or

@"<p>something</p>".AsHtml()
三寸金莲 2024-09-19 01:19:37

我在 Mono 下使用 ASP.NET MVC 和 Razor。

由于某些原因,我无法从 System.Web.Mvc 的 System.Web.WebPages 获取 HtmlHelper。

但在将模型的属性声明为 RazorEngine.Text.RawString 后,我设法输出未编码的字符串。现在它按预期输出。

示例

@{
    var txt = new RawString("some text with \"quotes\"");
    var txt2 = "some text with \"quotes\"";
}
<div>Here is unencoded text: @txt</div>
<div>Here is encoded text: @txt2</div>

输出:

<div>Here is unencoded text: some text with "quotes"</div>
<div>Here is encoded text: some text with "quotes"</div>

I'm using ASP.NET MVC and Razor under Mono.

I couldn't get HtmlHelper from System.Web.WebPages of System.Web.Mvc for some reasons.

But I managed to output unencoded string after declaring model's property as RazorEngine.Text.RawString. Now it outputs as expected.

Example

@{
    var txt = new RawString("some text with \"quotes\"");
    var txt2 = "some text with \"quotes\"";
}
<div>Here is unencoded text: @txt</div>
<div>Here is encoded text: @txt2</div>

Output:

<div>Here is unencoded text: some text with "quotes"</div>
<div>Here is encoded text: some text with "quotes"</div>
静赏你的温柔 2024-09-19 01:19:37

在将我们的项目转换到新的 Razor 视图引擎时,我也遇到了这个问题。我采取的方法略有不同,因为我们必须从 C# 生成 JSON 数据,并希望在页面加载时输出它。

我最终所做的是实现一个 RawView,它与 cshtml 文件内的 View 并行。本质上,要获取原始字符串,

@(new HtmlString(View.Foo))

// became
@RawView.Foo

这需要对项目布局进行一些更改,所以我刚刚写了一篇关于它的博客文章 此处。简而言之,这需要 MVC 的 DynamicViewDataDictionary 的重复实现和包含 RawView 的新 WebViewPage。我还继续在 RawView 上实现了索引运算符,以允许

@RawView["Foo"]

在有人需要使用键列表循环数据的情况下。

阅读护士的评论,如果我将其命名为 Literal 而不是 RawView 可能会更好。

I ran into this problem as well when transitioning our project to the new Razor view engine. The approach I took was slightly different because we had to generate JSON data from C# and wanted to output it upon page load.

What I eventually did was to implement a RawView that was a parallel of View inside of the cshtml files. Essentially, to get a raw string,

@(new HtmlString(View.Foo))

// became
@RawView.Foo

This requires a few changes to the project layout, so I just wrote up a blog post about it here. In short, this required a duplicate implementation of MVC's DynamicViewDataDictionary and a new WebViewPage that contains the RawView. I also went ahead and implemented the index operator on the RawView to allow for

@RawView["Foo"]

In the off-chance that someone needs to loop over the data with a list of keys.

Reading anurse's comment, it probably would have been better off if I had named this as a Literal instead of RawView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文