HttpUtility.UrlEncode in console application

2019-03-12 05: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.

7条回答
Lonely孤独者°
2楼-- · 2019-03-12 06:11

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...'

查看更多
放荡不羁爱自由
3楼-- · 2019-03-12 06:14

@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.

查看更多
放我归山
4楼-- · 2019-03-12 06:20

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).

查看更多
ら.Afraid
5楼-- · 2019-03-12 06:26

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

查看更多
Emotional °昔
6楼-- · 2019-03-12 06:27

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

查看更多
趁早两清
7楼-- · 2019-03-12 06:30

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

查看更多
登录 后发表回答