Why can't I find or use UrlEncode in Visual Studio 2010?
Asked Answered
E

6

15

I have a string that I'd like to encode into the standard URL format. From what I've found, I should be able to do this via the httpUtility.urlEncode method, but I don't seem to have that available.

I've added "using" references to both System.Web and System.Net to no avail. I've also seen other references to server.urlEncode amongst other variants, but I don't see the method anywhere.

I'm using the latest version of C# in Visual Studio 2010. Is the method called something different in this version, hidden somewhere else, or am I completely off base?

Erse answered 11/2, 2011 at 8:41 Comment(0)
T
29

By default, new projects in Visual Studio 2010 target the .NET Framework 4.0 Client Profile, which does not include the System.Web assembly.

You can change the version of the Framework that your project targets in your project's Properties. Under the "Application" tab, select ".NET Framework 4.0" from the combobox labeled "Target framework".

Then, make sure that you have added a reference to System.Web using the "Add Reference" dialog.

Finally, add a using directive to the top of your class for the System.Web namespace:

using System.Web;


You'll find the various overloads of the UrlEncode method in the HttpUtility class. Sample code:

HttpUtility.UrlEncode("http://www.google.com/");
Tomtoma answered 11/2, 2011 at 8:55 Comment(5)
As stated in my question, I have; although I should have code highlighted it so that it's more visible.Erse
@Sootah: You forgot to tell us what kind of project you've created.Tomtoma
Just a standard WinForms app.Erse
@Sootah: Then either you haven't followed the instructions in my answer carefully enough, or your computer is possessed. I promise it works; I tested it. Make sure you're targeting the full version of the Framework, not the Client Profile. It's bitten many programmers before.Tomtoma
@CodyGray I'm targeting Client Profile in Framework 4.0 and i found this as WebUtility.HtmlEncode. i imported Imports System.Web.Services.Rimose
R
4

In .Net 4.5 you can (should?, 'please use' says a Katana comment) use the System.Net.WebUtility.UrlEncode method.

Reba answered 21/2, 2014 at 11:26 Comment(0)
A
2

It can't be named differently since Visual Studio doesn't supply the class or method names, the .NET framework does.

All I can tell you is that the System.Web.HttpUtility AND System.Web.HttpServerUtility classes contain a method called UrlEncode(string).

Arola answered 11/2, 2011 at 8:47 Comment(6)
They contain several other overloads of that method, as well. And HttpServerUtility.UrlEncode simply calls HttpUtility.UrlEncode internally; they're the same method.Tomtoma
All that System.Web gives me are: AspNetHostingPermission, AspNetHostingPermissionAttribute, and AspNetHostingPermissionLevelErse
That would pretty strongly indicate that you're missing a reference to the System.Web dll, or that you're targeting the wrong profile.Duo
msdn.microsoft.com/en-us/library/w7f1y429.aspx (System.Web namespace) msdn.microsoft.com/en-us/library/4fkewx0t.aspx (HttpUtility class) msdn.microsoft.com/en-us/library/zttxte6w.aspx (HttpServerUtility class) Not according to MSDN.Arola
@TimS: Were you disagreeing with my last comment? You forgot to read the documentation you linked to. Specifically, this under the Remarks section: "UrlEncode is a convenient way to access the HttpUtility.UrlEncode method at run time from an ASP.NET application. Internally, UrlEncode uses HttpUtility.UrlEncode to encode strings."Tomtoma
No I was refering to Sootah, my badArola
I
2

If your project target ".NET Framework X Client Profile",you cannot not use "System.Web",but you can use "Uri.EscapeUriString | Uri.UnEscapeUriString" instead.

Innards answered 10/5, 2013 at 10:27 Comment(0)
L
1

Yes, adding the reference was my answer. But be sure you double check the project, that it is in, if you have more than 1 project in your solution. I had a solution with 3 projects. System.Web was added to 2 projects but not the 3rd project.

I spent an hour trying to figure out why I couldn't use HttpUtility since it was a Reference in the main project. But I didn't check the sub-projects of the Solution.

Hope it helps someone.

Liggitt answered 8/8, 2012 at 2:1 Comment(1)
I had the same issue. The Framework was 4.0 but the IDE could not recognize System.Net.WebUtility.UrlEncode method. It did only after I added System.web.dll to all dependent projects too and rebuilt them. Weird...Kilowatt
A
0

Because you only see AspNetHostingPermission, AspNetHostingPermissionAttribute, and AspNetHostingPermissionLevel, I strongly suspect (like the other guys) that you're missing a reference.

The best you can do is start a new project, because it's pretty complicated to add/remove references without ruining your entire project.

How to: Add or Remove References in Visual Studio (MSDN) shows how to add/remove references. In your case, you should check/add the System.Web reference.

Arola answered 11/2, 2011 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.