Unescape an escaped url in c#

后端 未结 2 868
梦如初夏
梦如初夏 2021-01-04 19:05

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

2条回答
  •  一整个雨季
    2021-01-04 19:32

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

提交回复
热议问题