开发者

Why cant i output HTML code?

开发者 https://www.devze.com 2023-04-13 01:38 出处:网络
I\'m trying to output HTML on my webpages by doing <p> @Server.HtmlEncode(elem.Text.ToString().Replace(\"[BR]\", \"<br />\"));

I'm trying to output HTML on my webpages by doing

  <p>
    @Server.HtmlEncode(elem.Text.ToString().Replace("[BR]", "<br />"));
  </p>

[BR] is my coded <br开发者_StackOverflow /> since i wont/cant save html directly. However, the problem is that when i use the @Server.HtmlEncode i get this output:

one little&lt;br /&gt;&lt;br /&gt;test!;

Without the encoding also looks wrong, it then output <br />'s in plaintext :(

Does anyone have a clue how to make it encode/output correctly?


You are double HTML encoding. @ already performs an Html encode. Try:

@elem.Text.ToString().Replace("[BR]", "<br />")

or:

@(new HtmlString(elem.Text.ToString().Replace("[BR]", "<br />")))

or:

@Html.Raw(elem.Text.ToString().Replace("[BR]", "<br />"))


The word Encode in HtmlEncode means it translates raw html to it's encoded output, ie what you are seeing. If you want raw html, then you do Response.Write() or, since it looks like you're using Razor, @Html.Raw().


HtmlEncode escapes special characters like < >. Use Html.Raw instead of if.

0

精彩评论

暂无评论...
验证码 换一张
取 消