3

I'm creating some html code from a cs file, which i want to be rendered one the actual site. But what I get rendered is the "raw" html code, not the rendered. What am I doing wrong?

the .cshtml code:

<div>
    @class1.createhtml();
</div>

the .cs code:

public static string createhtml()
{
    return "<p>hi</p>";
}
4

2 回答 2

14
<div>
    @Html.Raw(class1.createhtml())
</div>

您应该非常小心,不要将未转义的用户输入放入此字符串,否则会打开注入攻击的大门 - 请谨慎对待,您可能会处理附加字符串以进行 SQL 查询:如果在一切可能,还是要非常小心。

于 2013-08-13T13:55:22.433 回答
6

您应该在视图中使用Html.Raw方法:

@Html.Raw(class1.createhtml())
于 2013-08-13T13:55:15.000 回答