Unescape an escaped url in c#

15,206

Solution 1

& is an HTML entity and is used when text is encoded into HTML because you have to "escape" the & that has a special meaning in HTML. Apparently, this escaping mechanism was used on the URL presumably because it is used in some HTML for instance in a link. I'm not sure why you want to decode it because the browser will do the proper decoding when the link is clicked. But anyway, to revert it you can use HttpUtility.HtmlDecode in the System.Web namespace:

var encoded = "http://www.someurl.com/profile.php?mode=register&agreed=true";
var decoded = HttpUtility.HtmlDecode(encoded);

The value of decoded is:

http://www.someurl.com/profile.php?mode=register&agreed=true

Another form of encoding/decoding used is URL encoding. This is used to be able to include special characters in parts of the URL. For instance the characters /, ? and & have a special meaning in a URL. If you need to include any of these characters in a say a query parameter you will have to URL encode the parameter to not mess up the URL. Here is an example of an URL where URL escaping has been used:

http://www.someurl.com/profile.php?company=Barnes+%26+Noble

The company name Barnes & Noble was encoded as Barnes+%26+Noble. If the & hadn't been escaped the URL would have contained not one but two query parameters because & is used as a delimiter between query parameters.

Solution 2

not sure why but decode from @Martin's answer doesn't work in my case (filename in my case is "%D1%8D%D1%84%D1%84%D0%B5%D0%BA%D1%82%D0%B8%D0%BD%D0%BE%D0%B2%D1%81%D1%82%D1%8C%20%D0%BF%D1%80%D0%BE%D0%B4%D0%B0%D0%B6%202020%20(1)-8.xml").

For me works method - https://docs.microsoft.com/en-us/dotnet/api/system.uri.unescape?view=netcore-3.1 .

Be aware that this is obsolete.

Share:
15,206

Related videos on Youtube

Smith
Author by

Smith

Am a software and website developer, i design graphics and icons too!. I love programming, although i only consider myself an intermediate programmer. Hobbies I do alot of googling and ask alot of questions. I love watching movies (Not violent), listening to music (soft, blues etc), reading Others Am very good with my hands (meaning i like to do constructive work with them). If you have a programming problem, pls don't hesitate to ask me for help, i will do the best i can. May you have peace!

Updated on June 01, 2022

Comments

  • Smith
    Smith almost 2 years

    I have urls which is escaped in this form:

       http://www.someurl.com/profile.php?mode=register&agreed=true
    

    I want to convert it to unescaped form

       http://www.someurl.com/profile.php?mode=register&agreed=true
    

    is this the same thing as escapped html?

    how do i do this?

    thanks

    • futurecat
      futurecat
      URL encoding is completely different from HTML encoding, as this is marked duplicate of. Flagged for reopen.
  • Smith
    Smith almost 13 years
    this is a winforms application. will it work in .net 2.0? am getting an error HttpUtility' is not declared
  • Martin Liversage
    Martin Liversage almost 13 years
    @Smith: Here is the documentation for the .NET 2.0 HttpUtility class: msdn.microsoft.com/en-us/library/1e55w41w(v=VS.80).aspx. Did you remember to add a reference to System.Web.dll?