Alternative of Html.Raw in ASP.NET WebForms

2019-02-08 12:34发布

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

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?

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-02-08 12:49

You can use asp:Literal with Mode=PassThrough

查看更多
【Aperson】
3楼-- · 2019-02-08 13:02

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

or you can implement your own version of HTML.Raw

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

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);
    }
查看更多
登录 后发表回答