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?

Sootah
  • 1,771
  • 3
  • 23
  • 42

6 Answers6

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/");
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • As stated in my question, I have; although I should have code highlighted it so that it's more visible. – Sootah Feb 11 '11 at 09:02
  • @Sootah: You forgot to tell us what kind of project you've created. – Cody Gray - on strike Feb 11 '11 at 09:03
  • Just a standard WinForms app. – Sootah Feb 11 '11 at 09:04
  • @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. – Cody Gray - on strike Feb 11 '11 at 09:05
  • @CodyGray I'm targeting Client Profile in Framework 4.0 and i found this as `WebUtility.HtmlEncode`. i imported `Imports System.Web.Services`. – Sharky Aug 27 '13 at 12:16
4

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

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.

Liuliu
  • 21
  • 1
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).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tim S.
  • 13,597
  • 7
  • 46
  • 72
  • They contain [several other overloads](http://msdn.microsoft.com/en-us/library/s2x4538s.aspx) of that method, as well. And `HttpServerUtility.UrlEncode` simply calls `HttpUtility.UrlEncode` internally; they're the same method. – Cody Gray - on strike Feb 11 '11 at 08:50
  • All that System.Web gives me are: AspNetHostingPermission, AspNetHostingPermissionAttribute, and AspNetHostingPermissionLevel – Sootah Feb 11 '11 at 08:57
  • That would pretty strongly indicate that you're missing a reference to the System.Web dll, or that you're targeting the wrong profile. – Iain Galloway Feb 11 '11 at 09:07
  • http://msdn.microsoft.com/en-us/library/w7f1y429.aspx (System.Web namespace) http://msdn.microsoft.com/en-us/library/4fkewx0t.aspx (HttpUtility class) http://msdn.microsoft.com/en-us/library/zttxte6w.aspx (HttpServerUtility class) Not according to MSDN. – Tim S. Feb 11 '11 at 09:08
  • @TimS: Were you disagreeing with my last comment? You forgot to read the documentation you linked to. Specifically, [this](http://msdn.microsoft.com/en-us/library/zttxte6w.aspx) 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." – Cody Gray - on strike Feb 11 '11 at 09:11
  • No I was refering to Sootah, my bad – Tim S. Feb 11 '11 at 09:12
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.

Marko
  • 20,385
  • 13
  • 48
  • 64
rdouglass
  • 11
  • 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... – dmihailescu Aug 07 '15 at 16:31
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tim S.
  • 13,597
  • 7
  • 46
  • 72