147

How do i escape text for html use in C#? I want to do

sample="<span>blah<span>"

and have

<span>blah<span>

show up as plain text instead of blah only with the tags part of the html :(. Using C# not ASP

9 Answers9

191
using System.Web;

var encoded = HttpUtility.HtmlEncode(unencoded);
Michael S. Scherotter
  • 10,715
  • 3
  • 34
  • 57
  • 3
    If you also want to encode unicode characters to non-unicode, check out this: http://stackoverflow.com/questions/82008/non-unicode-xml-representation – Gyuri Dec 04 '09 at 18:14
  • 5
    Something that you don't want to find out the bad way: The above method by itself does not escape control characters. See the accepted answer here: http://stackoverflow.com/a/4501246/1543677 and use both. – pkExec Dec 09 '14 at 11:46
  • HttpUtility does not exist anymore (win store apps) – Tertium Nov 12 '16 at 10:04
91

Also, you can use this if you don't want to use the System.Web assembly:

var encoded = System.Security.SecurityElement.Escape(unencoded)

Per this article, the difference between System.Security.SecurityElement.Escape() and System.Web.HttpUtility.HtmlEncode() is that the former also encodes apostrophe (') characters.

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Tereza Tomcova
  • 4,928
  • 4
  • 30
  • 29
59

If you're using .NET 4 or above and you don't want to reference System.Web, you can use WebUtility.HtmlEncode from System

var encoded = WebUtility.HtmlEncode(unencoded);

This has the same effect as HttpUtility.HtmlEncode and should be preferred over System.Security.SecurityElement.Escape.

Alex
  • 7,728
  • 3
  • 35
  • 62
  • Why should it be preferred over SecurityElement.Escape? Are there vulnerabilities in the latter, or is the former just more capable? – Travis Dec 18 '13 at 22:43
  • 9
    @Travis There are no vulnerabilities in either, it's just that `SecurityElement.Escape` operates on XML and `HtmlEncode` operates on HTML, and XML and HTML encoding have slightly different requirements (see [this answer](http://stackoverflow.com/a/2083770/175157) for details). So, for example, `SecurityElement.Escape` is allowed to use `'`, while `HtmlEncode` is not. – Alex Dec 19 '13 at 09:38
  • 1
    @Travis I think the even better "excuse" is that **System.Net is available to Portable Class Libraries** and the other two options aren't/don't seem to be this morning. ;^) – ruffin Dec 28 '16 at 12:43
19

In ASP.NET 4.0 there's new syntax to do this. Instead of

<%= HttpUtility.HtmlEncode(unencoded) %>

you can simply do

<%: unencoded %>

Read more here:

New <%: %> Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nacht
  • 3,342
  • 4
  • 26
  • 41
6

.NET 4.0 and above:

using System.Web.Security.AntiXss;
//...
var encoded = AntiXssEncoder.HtmlEncode("input", useNamedEntities: true);
Victor
  • 3,669
  • 3
  • 37
  • 42
5

You can use actual html tags <xmp> and </xmp> to output the string as is to show all of the tags in between the xmp tags.

Or you can also use on the server Server.UrlEncode or HttpUtility.HtmlEncode.

Andrew Siemer
  • 10,166
  • 3
  • 41
  • 61
  • I made the question more clear. I dont want the tags to be part of html as the user can do and break it. –  Jun 17 '09 at 05:36
  • 1
    `` has been deprecated long ago: http://stackoverflow.com/questions/8307846/why-was-the-xmp-html-tag-deprecated use `<pre>` instead</pre> – mortb May 11 '17 at 14:19
2

For a simple way to do this in Razor pages, use the following:

In .cshtml:

@Html.Raw(Html.Encode("<span>blah<span>"))

In .cshtml.cs:

string rawHtml = Html.Raw(Html.Encode("<span>blah<span>"));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
fordrof
  • 107
  • 6
1

You can use:

System.Web.HttpUtility.JavaScriptStringEncode("Hello, this is Satan's Site")

It was the only thing that worked (ASP.NET 4.0+) when dealing with HTML like this. The&apos; gets rendered as ' (using htmldecode) in the HTML content, causing it to fail:

<a href="article.aspx?id=268" onclick="tabs.open('modules/xxx/id/268', 'It&apos;s Allstars'); return false;">It's Allstars</a>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Contra
  • 2,754
  • 4
  • 20
  • 18
1

There are some special quotes characters which are not removed by HtmlEncode and will not be displayed in Edge or Internet Explorer correctly, like and . You can extend replacing these characters with something like the below function.

private string RemoveJunkChars(string input)
{
    return HttpUtility.HtmlEncode(input.Replace("”", "\"").Replace("“", "\""));
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Iman
  • 17,932
  • 6
  • 80
  • 90
  • You are probably serving content using the wrong encoding. IE and Edge have no problems displaying such characters. – Bouke Feb 18 '20 at 09:57