简体   繁体   English

在 c# 中取消转义转义的 url

[英]Unescape an escaped url in c#

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?这和逃逸的 html 是一样的吗?

how do i do this?我该怎么做呢?

thanks谢谢

& 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.是一个 HTML 实体,在文本编码为 HTML 时使用,因为您必须“转义”在 HTML 中具有特殊含义的& Apparently, this escaping mechanism was used on the URL presumably because it is used in some HTML for instance in a link.显然,这种 escaping 机制被用于 URL 大概是因为它在某些 HTML 中使用,例如在链接中。 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:但无论如何,要恢复它,您可以在System.Web命名空间中使用HttpUtility.HtmlDecode

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

The value of decoded is: decoded的值为:

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

Another form of encoding/decoding used is URL encoding.使用的另一种编码/解码形式是 URL 编码。 This is used to be able to include special characters in parts of the URL.这用于能够在 URL 的部分中包含特殊字符。 For instance the characters / , ?例如字符/ , ? and & have a special meaning in a URL.&在 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.如果您需要在查询参数中包含这些字符中的任何一个,则必须对参数进行 URL 编码,以免弄乱 URL。 Here is an example of an URL where URL escaping has been used:以下是 URL 的示例,其中使用了 URL escaping:

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

The company name Barnes & Noble was encoded as Barnes+%26+Noble .公司名称Barnes & Noble编码为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.如果&没有被转义,则 URL 将包含不是一个而是两个查询参数,因为&用作查询参数之间的分隔符。

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").不知道为什么,但从@Martin的答案解码在我的情况下不起作用(在我的情况下,文件名是“%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 .对我来说工作方法 - https://docs.microsoft.com/en-us/dotnet/api/system.uri.unescape?view=netcore-3.1

Be aware that this is obsolete.请注意,这已过时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM