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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2146256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:50:46  来源:igfitidea点击:

HttpUtility.UrlEncode in console application

.netvb.netvisual-studio-2010console-applicationurlencode

提问by George

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 questionAnjisan suggests to add a reference to System.Web, as follows:

这个问题中,安吉散建议添加对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
  4. Scroll down to System.Web, select that, and hit ok
  1. 在您的解决方案资源管理器中,右键单击引用
  2. 选择“添加参考”
  3. 在“添加引用”对话框中,使用 .NET 选项卡
  4. 向下滚动到 System.Web,选择它,然后点击确定

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

但是,我在该位置没有 System.Web 条目。

回答by Rubens Farias

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

System.Web在那里,但似乎 Visual Studio 默认不按名称排序。您需要单击Component name列以对该列表进行排序。

I was able to run this code:

我能够运行此代码:

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

Just adding a System.Webreference, withoutSystem.Web.Extensions.

只是添加一个System.Web引用,没有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 Client Profile 替换为 .NET Framework 4;转到项目>>属性...>>应用程序选项卡

回答by Rush Frisby

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

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

回答by Lirrik

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

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

回答by Ion Todirel

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

@iar,框架没有设置为“错误的”。默认情况下,Visual Studio 2010 中的控制台和 Windows 应用程序(还有更多)面向 .NET Framework 4 Client Profile。这是设计使然。当您在“新建项目”对话框中定位“.NET Framework 4”时会发生这种情况。为什么它没有找到 System.Web.dll 是因为它不是 .NET Framework 4 Client Profile 的一部分,所以项目系统找不到它并在构建时发出警告(你可能忽略了,授予你引用System.Web.dll)。您也不会在“添加引用”对话框的 .NET 选项卡中找到 System.Web.dll,因为该列表是根据目标框架过滤的。

回答by Jarrett Meyer

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

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

回答by Kiran Bheemarti

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.

将项目的目标框架从“.NET Framework 4 Client Profile”更改为“.NET Framework 4.0”对我有帮助,将 .NET Framework 4 Client Profile 作为目标框架,我能够通过浏览到 C:\ 添加 System.Web 2.0 版本WINDOWS\Microsoft.NET\Framework\v2.0.50727 来自添加参考窗口,但对 System.Web 4.0 dll 执行相同操作时发出警告。

回答by Bob

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

对于 Visual Studio 2010 中的 VB 项目,将目标框架从“.NET Framework 4.0 Client Profile”更改为“.NET Framework 4.0”的选项位于“项目”>“属性”>“编译”>“高级编译选项...”下