ASP.NET WebForms 中 Html.Raw 的替代方案 [英] Alternative of Html.Raw in ASP.NET WebForms

查看:25
本文介绍了ASP.NET WebForms 中 Html.Raw 的替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误一个潜在的危险请求".. 在 Web 表单应用程序中,我尝试使用validatepage=false"和",然后我尝试了 Server.HtmlEncode,因此它将编码的 html 保存在数据库中.现在,当我通过 Server.HtmlDecode(DataContent.FieldValue("Contents", Container)) 在中继器控件中显示数据时,它显示了带有 html 标签的文本,例如 <p>asfd</p>...

I was getting error "A potential dangerous request" .. in Web Form application I have tried with "validatepage=false" and "" then i tried Server.HtmlEncode so it is saving encoded html in database. Now when i showed the data in Repeater control by Server.HtmlDecode(DataContent.FieldValue("Contents", Container)) It is showing text with html tags like <p>asfd</p>..

我该如何解决这个问题?在剃刀视图中 Html.Raw 工作正常,但在 webform 视图/ASP.NET 中有什么替代方案?有人可以帮忙吗?

how i can resolve this issue? In razor view Html.Raw works fine but what is alternative in webform view / ASP.NET? Can anybody help?

推荐答案

你可以使用 <%= value %> 这将对值进行编码.

you can use <%= value %> which will not encode the value.

或者您可以实现自己的 HTML.Raw 版本

or you can implement your own version of HTML.Raw

Html.Raw 返回一个与字符串几乎相同的 IHtmlString 实例,但 ASP.net 不编码 IHtmlString.

Html.Raw returns an IHtmlString instance which is almost same as string but ASP.net doesn't encode IHtmlString.

复制 HTML.Raw() 的简单函数

Simple function to replicate HTML.Raw()

    /// <summary>
    /// Stops asp.net from encoding the source HTML string.
    /// </summary>
    /// <param name="source"></param>
    /// <returns></returns>
    public static IHtmlString HTMLRaw(string source)
    {
        return new HtmlString(source);
    }

这篇关于ASP.NET WebForms 中 Html.Raw 的替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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