tags:

views:

775

answers:

6

I want to make a string into a URL using C#, there must be something in the .NET framework that should help, right?.

Thanks. Haim.

+15  A: 

I believe you're looking for HttpServerUtility.UrlEncode.

System.Web.HttpUtility.UrlEncode(string url)
LiraNuna
Actually `Uri.EscapeDataString` is the appropriate method to use. HttpUtility.UrlEncode encodes the spaces with `+` and not `%20` as requested in the question.
Nip
+1  A: 

Use HttpServerUtility.UrlEncode

Thomas Levesque
A: 

HttpServerUtility.HtmlEncode

From the docs:

String TestString = "This is a <Test String>.";
String EncodedString = Server.HtmlEncode(TestString);

Update: This actually encodes Html not Urls. Use UrlEncode(TestString) as everyone who can read better than me said. Not deleting this on the off chance someone finds this thread while looking for html encoding.

George Mauer
The OP is asking about URL Encoding; not HTML Encoding.
Metro Smurf
No, this is for HTML encoding, not URL encoding (as the name implies...)
Thomas Levesque
Yup, you're right, use UrlEncode() for encoding urls - this is for encoding Html Entities only
George Mauer
A: 

HttpUtility.UrlEncode Method (String)

Jacob
+4  A: 

Another way of doing this is using Uri.EscapeUriString(stringToEscape).

0xA3
+2  A: 

I found useful System.Web.HttpUtility.UrlPathEncode(string str);

It replaces spaces with %20 and not with +.

Gilda