HttpUtility not recognised in .Net 4.5
Asked Answered
M

5

29

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?)
Mckinnon answered 12/1, 2015 at 8:56 Comment(0)
W
59

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.
Wadi answered 12/1, 2015 at 9:0 Comment(0)
E
19

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.

Exuviae answered 3/7, 2017 at 10:9 Comment(0)
T
3

The HttpUtility class exists from .NET 1.1, so I think it is not possible for regular projects to 'not see it', as long as you have included a reference to System.Web.

You might be using a PCL (Portable Class Library), which uses a stripped down version of the framework that is supported on the platforms you selected, like Windows Store apps, Windows Phone, Silverlight, etc.

Thar answered 12/1, 2015 at 8:58 Comment(0)
T
2

I hope this link will help you. http://msdn.microsoft.com/en-us/library/system.web.httputility(v=vs.110).aspx Dot net framework 4.5 support HttpUtility as it is under System.Web namespace. Also adding a System.Web reference, without System.Web.Extensions reference into your project. If it doesn't work remove the existing and add new reference of System.Web into project. Also check which framework it is targeting it should be .NET Framework 4 or 4.5 without Client.

Thurnau answered 12/1, 2015 at 9:33 Comment(0)
M
1

I encountered this issue in .net 4.5.2 (using VS2019). I did check that I was using full framework and I also tried explicitly declaring System.Web in a using statement, though VS complains that the using clause is not needed.

System.Web.Utility appears to have been replaced by System.Net.Webutility

Musicianship answered 13/7, 2020 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.