Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpUtility not recognised in .Net 4.5

I Developed a WinForm application in with the target framework set to .net 4.0, now I wish to add to a project that has it's target framework set to .net 4.5. After I added the 4.0 WinForm application to my 4.5 project I keep getting the an error on my HttpUtility object.

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

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

I did include the System.Web namespace where the HttpUtility is located.

Visual Studio Error:

CS0234 The type or namespace name 'HttpUtility' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) 
like image 500
Marnus_Steyn Avatar asked Jan 12 '15 08:01

Marnus_Steyn


People also ask

What is http utility?

This utility provides a simple HTTP client interface that enables you to post messages/requests to HTTP servers, which is useful for orchestrations that have an HTTP Receive Request activity as the starter activity.

What is .NET system Web?

Defines the methods, properties, and events that are common to all application objects in an ASP.NET application. This class is the base class for applications that are defined by the user in the Global.


2 Answers

The problem is somewhere else.

As you can see in MSDN the HttpUtility class is present in System.Web in .NET Framework 4.5.

You're probably targeting the Client Profile: target the full framework in Project Properties. Otherwise:

  • either you did not add the right using statement using System.Web;
  • or you did not add the reference to System.Web.dll in the project.
like image 167
giammin Avatar answered Sep 17 '22 15:09

giammin


WebUtility

You also have another possibility: Use the WebUtility class. The WebUtility class is recommended by Microsoft itself and should be used outside of web applications.

Like the HttpUtility class it also provides you with the possibility to encode and decode URLs.

This way you don't have the problems with importing the library into your project or setting some specific profiles.


From the Documentation (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.

like image 32
kamwo Avatar answered Sep 18 '22 15:09

kamwo