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

5 Answers 5

32

HttpUtility.UrlEncode

Sign up to request clarification or add additional context in comments.

1 Comment

UrlEncode() and UrlPathEncode() do not encode all special characters.
13
System.Net.WebUtility.UrlEncode(str)

3 Comments

This method doesn't exists (There are HtmlEncode and HtmlDecode only).
link Its .Net Framework 4.5
This is actually a very good solution since it does not have a dependency on System.Web and can also be used in PCLs.
5

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

Comments

1

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

1 Comment

I get an error Error... The type 'System.Web.HttpServerUtility' has no constructors defined. How do you new it up then?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.