简体   繁体   中英

HttpUtility.UrlEncode in console application

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

System.Web.HttpUtility.UrlEncode(item)

Error message:

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.

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

确保您的目标框架是“.NET Framework 4”而不是“.NET Framework 4 Client Profile”。

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

如果您使用的是.NET 4.0或更高版本,则可以使用与客户端配置文件一起使用的WebUtility.UrlEncode (不需要System.Web程序集引用)。

您需要添加对System.WebSystem.Web.Extensions的引用。

将项目的目标框架从“.NET Framework 4 Client Profile”更改为“.NET Framework 4.0”帮助我,使用.NET Framework 4 Client Profile作为Target框架,我能够通过浏览到C:\\添加System.Web 2.0版本添加引用窗口的WINDOWS \\ Microsoft.NET \\ Framework \\ v2.0.50727但对System.Web 4.0 dll做同样的事情发出了警告。

对于Visual Studio 2010中的VB项目,将目标框架从“.NET Framework 4.0 Client Profile”更改为“.NET Framework 4.0”的选项位于Project> Properties> Compile>'Advanced Compile Options ...'下。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM