.Net 4.5 无法识别 HttpUtility

HttpUtility not recognised in .Net 4.5

我开发了一个目标框架设置为 .net 4.0 的 WinForm 应用程序,现在我想添加到目标框架设置为 .net 4.5 的项目中。在我将 4.0 WinForm 应用程序添加到我的 4.5 项目后,我的 HttpUtility 对象上不断出现错误。

 data += "&batch_data=" + HttpUtility.UrlEncode(batch, System.Text.Encoding.GetEncoding("ISO-8859-1"));

"The name 'HttpUtility' does not exist in the current context"

我确实包含了 HttpUtility 所在的 System.Web 命名空间。

Visual Studio 错误:

CS0234 The type or namespace name 'HttpUtility' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)

.NET 1.1 中存在 HttpUtility class,因此我认为常规项目不可能 'not see it',只要您包含对 System.Web.

您可能正在使用 PCL (Portable Class Library),它使用您选择的平台支持的精简版框架,例如 Windows 商店应用, Windows Phone、Silverlight 等

问题出在其他地方。

如您在 MSDN 中所见,HttpUtility class 存在于 .NET Framework 4.5 的 System.Web 中。

您可能以客户配置文件为目标:以项目属性中的完整框架为目标。否则:

  • 要么你没有添加正确的using语句using System.Web;
  • 或者你没有在项目中添加对System.Web.dll的引用。

希望这篇 link 对您有所帮助。 http://msdn.microsoft.com/en-us/library/system.web.httputility(v=vs.110).aspx Dot net framework 4.5 支持 HttpUtility,因为它位于 System.Web 命名空间下。 还添加了 System.Web 引用,但没有 System.Web.Extensions 引用到您的项目中。如果它不起作用,请删除 System.Web 的现有引用并将新引用添加到项目中。还要检查它针对的是哪个框架。它应该是没有客户端的 .NET Framework 4 或 4.5。

网络实用程序

你还有另一种可能:使用WebUtility classWebUtility class 是 Microsoft 自己推荐的,应该在 Web 应用程序之外使用。

HttpUtility class 一样,它还为您提供了对 URL 进行编码和解码的可能性。

这样您就不会遇到将库导入项目或设置某些特定配置文件的问题。


来自文档 (Source)

The HttpUtility class is used internally by the HttpServerUtility class, whose methods and properties are exposed through the intrinsic ASP.NET Server object. Additionally, the HttpUtility class contains encoding and decoding utility methods that are not accessible from the Server.

To encode or decode values outside of a web application, use the WebUtility class.

我在 .net 4.5.2(使用 VS2019)中遇到了这个问题。我确实检查过我是否使用了完整的框架,并且我还尝试在 using 语句中显式声明 System.Web,尽管 VS 抱怨不需要 using 子句。

System.Web。实用程序似乎已被 System.Net.Webutility

取代