32

I'd like to use HttpUtility.UrlEncode in a console application, VB.NET, VS 2010 Beta 2.

System.Web.HttpUtility.UrlEncode(item)

Error message: 'HttpUtility' is not a member of 'Web'.

In this question Anjisan suggests to add a reference to System.Web, as follows:

  1. In your solution explorer, right click on references
  2. Choose "add reference"
  3. In the "Add Reference" dialog box, use the .NET tab
  4. Scroll down to System.Web, select that, and hit ok

However, I don't have a System.Web entry at that location.

Community
  • 1
  • 1
George
  • 1,111
  • 3
  • 20
  • 38

7 Answers7

32

System.Web is there, but seems Visual Studio doesn't sort by name by default. You'll need to click Component name column to get that list sorted.

I was able to run this code:

Console.WriteLine(System.Web.HttpUtility.UrlEncode(" ")); // +

Just adding a System.Web reference, without System.Web.Extensions.

Also, You should replace your target framework from .NET Framework 4 Client Profile to .NET Framework 4; go to Project >> Properties... >> Application tab

Peter Perháč
  • 20,434
  • 21
  • 120
  • 152
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • 1
    I still can't find it. I only have "System.Web.ApplicationServices" and "System.Web.Services". Which version of Visual Studio / .NET are you using? – George Jan 27 '10 at 11:44
  • I tested with VS2010b2; did you sorted that list? What project type, framework version did you defined? – Rubens Farias Jan 27 '10 at 12:16
  • I did sort the list. It's a console application. Framework 4.0, runtime version v4.0.21006. – George Jan 27 '10 at 13:03
  • 9
    You should to replace your target framework from `.NET Framework 4 Client Profile` to `.NET Framework 4`; go to `Project, {0} Properties..., Application tab` – Rubens Farias Jan 27 '10 at 13:24
  • You are a hero. I have no idea how this target framework was set to "the wrong one", but anyway it works now. THANK YOU VERY MUCH. – George Jan 27 '10 at 13:48
  • 1
    In visual studio 2010, the default project is "4.0 client profile", so it's not 'wrong'. – Frederik Aug 19 '10 at 15:22
  • 1
    You then (after changing from the correct default console app .net configuration of client profile, to the .net framework 4) MUST click on References and add the System.Web, then will will no longer receive the error message. – Tom Stickel Nov 17 '11 at 21:16
14

Make sure your target framework is ".NET Framework 4" not ".NET Framework 4 Client Profile".

Rush Frisby
  • 11,388
  • 19
  • 63
  • 83
  • 2
    This is the correct answer, but you may need to explicitly add `System.Web` as a reference (under Project > Add Reference) – arserbin3 Aug 19 '14 at 15:21
6

If you are using .NET 4.0 or later, you can use WebUtility.UrlEncode which works with client profile (does not require System.Web assembly reference).

Lirrik
  • 795
  • 11
  • 17
5

@iar, the framework was not set to "the wrong one". Console and Windows applications (there are more) target .NET Framework 4 Client Profile by default in Visual Studio 2010. This is by design. And it happens when you target ".NET Framework 4" in the New Project Dialog. Why it didn't find System.Web.dll is because it is not part of the .NET Framework 4 Client Profile, so the project system cannot find it and issues a warning during build-time (which you probably ignored, granting you referenced System.Web.dll). You won't find System.Web.dll in the .NET tab in Add Reference dialog also, because that list is filtered according to the targeted framework.

1

You need to add a reference to both System.Web and System.Web.Extensions.

Jarrett Meyer
  • 19,333
  • 6
  • 58
  • 52
  • I only needed to add a reference to System.Web. Even though System.Web was already mentioned in Imports and no error, explicitly adding a reference to it fixed it – user2728841 Dec 16 '20 at 13:12
1

Changing the project's Target Framework from ".NET Framework 4 Client Profile" to ".NET Framework 4.0" helped me, with .NET Framework 4 Client Profile as Target framework I was able to add System.Web 2.0 version by Browsing to C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 from Add reference window but doing the same for System.Web 4.0 dll gave a warning.

Kiran Bheemarti
  • 1,439
  • 2
  • 12
  • 14
1

For VB projects in Visual Studio 2010, the option to change the target framework from ".NET Framework 4.0 Client Profile" to ".NET Framework 4.0" is under Project > Properties > Compile > 'Advanced Compile Options...'

Bob
  • 11
  • 1