简体   繁体   English

控制台应用程序中的HttpUtility.UrlEncode

[英]HttpUtility.UrlEncode in console application

I'd like to use HttpUtility.UrlEncode in a console application, VB.NET, VS 2010 Beta 2. 我想在控制台应用程序,VB.NET,VS 2010 Beta 2中使用HttpUtility.UrlEncode。

System.Web.HttpUtility.UrlEncode(item)

Error message: 'HttpUtility' is not a member of 'Web'. 错误消息: 'HttpUtility'不是'Web'的成员。

In this question Anjisan suggests to add a reference to System.Web, as follows: 这个问题中, Anjisan建议添加对System.Web的引用,如下所示:

  1. In your solution explorer, right click on references 在解决方案资源管理器中,右键单击引用
  2. Choose "add reference" 选择“添加参考”
  3. In the "Add Reference" dialog box, use the .NET tab 在“添加引用”对话框中,使用.NET选项卡
  4. Scroll down to System.Web, select that, and hit ok 向下滚动到System.Web,选择它,然后点击确定

However, I don't have a System.Web entry at that location. 但是,我在该位置没有System.Web条目。

System.Web is there, but seems Visual Studio doesn't sort by name by default. System.Web就在那里,但似乎Visual Studio默认情况下不按名称排序。 You'll need to click Component name column to get that list sorted. 您需要单击“ Component name列以对该列表进行排序。

I was able to run this code: 我能够运行这段代码:

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

Just adding a System.Web reference, without System.Web.Extensions . 只需添加System.Web引用, 无需 System.Web.Extensions

Also, You should replace your target framework from .NET Framework 4 Client Profile to .NET Framework 4; 此外,您应该将目标框架从.NET Framework 4 Client Profile替换为.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". @ iar,框架没有设置为“错误的”。 Console and Windows applications (there are more) target .NET Framework 4 Client Profile by default in Visual Studio 2010. This is by design. 默认情况下,Visual Studio 2010中的控制台和Windows应用程序(还有更多)定位.NET Framework 4 Client Profile。这是设计使然。 And it happens when you target ".NET Framework 4" in the New Project Dialog. 当你在New Project Dialog中定位“.NET Framework 4”时会发生这种情况。 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). 为什么它没有找到System.Web.dll是因为它不是.NET Framework 4 Client Profile的一部分,所以项目系统找不到它并在构建期间发出警告(您可能忽略了它,授予您引用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”选项卡中找到System.Web.dll,因为该列表是根据目标框架过滤的。

如果您使用的是.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 ...'下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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