关闭HTML编码在剃刀 [英] Turn off HTML Encoding in Razor

查看:91
本文介绍了关闭HTML编码在剃刀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回一段JavaScript代码的函数和/或HTML。

I have a function that returns a snippet of JavaScript and/or HTML.

static public string SpeakEvil()
{
    return "<script>alert('BLAH!!');</script>";
}

在视图中,剃刀是相当正确的HTML编码它,最期望的那样。

In the view, Razor is quite rightly HTML encoding it, as most would expect.

@StaticFunctions.SpeakEvil()

我如何有剃刀的不可以 HTML恩code这一点,从而使HTML和JavaScript逐字发出的,而任何脚本实际运行?

How do I have Razor not HTML Encode this, so that the HTML and JavaScript are emitted verbatim, and that any script actually runs?

推荐答案

您可以使用原()的功能,但它主要意味着东西来自数据库。

You could use the Raw() function but it's mostly meant for things that come from the database.

有关帮手像你这样我会建议返回一个 IHtmlString

For a helper like you have I would suggest returning an IHtmlString:

static public IHtmlString SpeakEvil() {
    return new HtmlString("<script>alert('BLAH!!');</script>");
}

你不必这样得叫原()在每个调用点。

这篇关于关闭HTML编码在剃刀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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