简体   繁体   中英

C# mvc2 encode url

i'm trying to encode an url with the code below;

var encodedUrl = HttpUtility.UrlEncode("http://www.example.com");
var decodedUrl = HttpUtility.UrlDecode("http%3A%2F%2Fwww%2Eexample%2Ecom%2F");

I'm working with the google webmaster tools api and this api expects an URL as shown in the decodedUrl variable above. Every single character is encoded there.

When i use the httputility encode function i get the following result;

http%3a%2f%2fwww.example.com

How can i use the encoding variable in such a way that every character in the url is encoded?

I'm pretty sure that HtmlUtility and AntiXss (another MS tool for encoding urls) aren't going to help here. A "." in a url is considered valid and so doesn't need to be encoded.

I think you're going to have to post-process your encoded string to further encode other characters that are not valid within teh google webmaster tools API.

ie do something like this...

var encodedUrl = HttpUtility.UrlEncode("http://www.example.com")
                            .Replace(".", "%2E");

... assuming that "." is the only character you're having problems with.

The period is not a reserved character in a URL, so it won't be encoded. See this question and answer for an elegant solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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