Decode escaped Url without using HttpUtility.UrlDe

2019-01-14 05:48发布

Is there any function that converts an escaped Url string to its unescaped form? System.Web.HttpUtility.UrlDecode() can do that job but I don't want to add a reference to System.Web.dll. Since my app is not a web application, I don't want to add a dependency for only using a function in an assembly.

UPDATE: Check Rick Strahl's blog post about the same issue.

标签: .net url
7条回答
混吃等死
2楼-- · 2019-01-14 06:30

@Smith
I was having the save problem. No changes or just further jumbling.

After testing many things I noticed a test string did decode. Ultimately I had to create a new empty string, set it's value to the encoded string then run WebUtility.HtmlDecode and Uri.UnescapeDataString on the new string. For some reason I had to run the decode and unescape in the order I mentioned. Bizarre.

I solved it with something like this.

Dim strEncoded as string="http%3a%2f%2fwww.google.com%2fsearch%3fhl%3den%26q%3dsomething%20%2323%26btnG%3dGoogle%2bSearch%26aq%3df%26oq%3d"

Dim strDecoded as string = ""
strDecoded = strEncoded
strDecoded = WebUtility.HtmlDecode(strDecoded)
strDecoded = Uri.UnescapeDataString(strDecoded)
查看更多
登录 后发表回答