Lenny Will
Lenny Will

Reputation: 689

HttpUtility.HtmlDecode returns same string

this maybe sounds like an especially stupid question but in my asp.net-project I use HttpUtility.HtmlDecode to decode following string:

string bar = HttpUtility.HtmlDecode("%3CFoobar%3E");

But the resulting string is the same exact one as before. What am I doing wrong?

Upvotes: 1

Views: 1822

Answers (1)

Peter Csala
Peter Csala

Reputation: 22679

The HttpUtility class supports two types of encoding/decoding:

  • URL
  • HTML

You have tried to decode an URL encoded string with HTML decoder.

Either use HTML encoding/decoding

const string original = "<Foobar>";
string encoded = HttpUtility.HtmlEncode(original); //&lt;Foobar&gt;
string decoded = HttpUtility.HtmlDecode(encoded); //<Foobar>

OR use URL encoding/decoding

const string original = "<Foobar>";
string encoded = HttpUtility.UrlEncode(original); //%3CFoobar%3E
string decoded = HttpUtility.UrlDecode(encoded); //<Foobar>

Upvotes: 5

Related Questions