-->

C# method to do URL encoding?

2020-08-09 11:15发布

问题:

what c# class method can I use to URL encode a URL string?

In my use case I want to pass a URL string as a URL parameter itself. So like burying a URL within a URL. Without some encoding the "&" and "?" characters in the inner URL can get picked up when the parameters for the outer Url parameters are processed

thanks

回答1:

HttpUtility.UrlEncode



回答2:

System.Net.WebUtility.UrlEncode(str)


回答3:

HttpServerUtility.UrlEncode might be exactly what you are looking for.



回答4:

Use Server.UrlEncode

You need an instance of the HttpServerUtility class, because the UrlEncode method is not static.

See http://msdn.microsoft.com/en-us/library/system.web.httpserverutility(v=VS.90).aspx



回答5:

I am operating with .net framework 2.0. But it never shows me "UrlEncode" intelisense when I place dot after httpserverutility. Here are the options I see:

HttpServerUtility.Equals 
HttpServerUtility.ReferenceEquals
HttpServerUtility.UrlTokenDecode
HttpServerUtility.UrlTokenEncode

That's it, these are the only options I see. Could I possibly ask for small piece of code as an example to use url encode function?

Thanks,

Linda