HttpUtility not recognised in .Net 4.5 - c#

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?)

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.

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.

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.

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.

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

Related

Adding CsWinRT nuget breaks using Windows.System namespace

I've created a .NET 5.0 project, one of the dependencies I have is on this API:
AnalyticsInfo.VersionInfo.DeviceFamily
After installing Microsoft.Windows.SDK.Contracts, I'm able to use this API. Then, I needed to install the Microsoft.Windows.CsWinRT package to resolve this error:
Error NETSDK1130 Referencing a Windows Metadata component directly when targeting
.NETCoreApp,Version=v5.0 is not supported.
Use the C#/WinRT projection tool (https://aka.ms/cswinrt) or a provided projection for this target.
After installing this, I no longer have access to the Windows.System.Profile namespace to call the AnalyticsInfo API:
Error CS0234 The type or namespace name 'System' does not exist in the namespace 'Windows' (are you missing an assembly reference?)
With .NET 5, built-in support for WinRT APIs in .NET is removed (because it's Windows specific) , so we can't use Microsoft.Windows.SDK.Contracts any more.
The solution as explained here Built-in support for WinRT is removed from .NET is to
Remove references to the Microsoft.Windows.SDK.Contracts package.
Instead, specify the version of the Windows APIs that you want to
access via the TargetFramework property of the project. For example:
<TargetFramework>net5.0-windows10.0.19041</TargetFramework>
Note with that in place, there's no need to manually add a reference to C#/WinRT (Microsoft.Windows.CsWinRT) it should be done automatically and shown as "Microsoft.Windows.SDK.NET.Ref" in the list of Frameworks Dependencies.

Can't add System.Drawing namespace in C# console application

I'm trying to add the System.Drawing namespace in my C# console application but when I go to use it's 'Image' type, I get the error:
"The type name 'Image' could not be found it the namespace 'System.Drawing'. This type has been forwarded to assembly System.Drawing.Common, Version=4.0.20, Culture=neutral, Consider adding reference to that assembly".
I have already went to Project>Add Reference>COM>System.Drawing.dll>Select>OK to add it but the error is still there.
Don't use the "COM" section in that dialog. Use "Assemblies" instead:
Note how this gives newer versions (4.0) instead of old versions (2.0 / 2.4).
If there is no "Assemblies" section, check that you really created a C# .NET Framework project. Likely you chose C# .NET Core.
If you want to stick with .NET Core, use the Nuget package System.Drawing.Common

Namespace of Regex won't work

Im refurbishing some old code that used to work on .NET Framework 3.5 to make it work on .NET Framework 4 using C#.
The following Regex used to work fine with version 3.5 but doesn't work anymore for some strange reason.
public static readonly Regex ChatColorRegex = new Regex("\\|c[A-Za-z0-9]{6,8}"),
ChatLinkRegex = new Regex("\\|H.*?\\|h");
I have added the 'using System.Text.RegularExpressions' at the top of my file, but the following error rises: 'The type or namespace RegularExpressions does not exist in the namespace System.Text.
I've googled about that and read that you have to add a Reference to System.Text.RegularExpressions in Visual Studio. However, when i did, i couldn't find System.Text.RegularExpressions in the list of References i could add.
I'm using Visual Studio 2012.
Could anyone tell me what im doing wrong, or forget to read?
The Regex class is still in the System.Text.RegularExpression namespace. The class is in the System assembly.
If you check your project references in Solution Explorer, you should see a reference to the System assembly. Check the properties of that reference to see what .NET Framework version is being used for the System assembly reference. It should match the .NET Framework version you selected for the "Target framework" in the project properties (Application tab).
Edit: The Regex class is in the System.Text.RegularExpressions namespace, not System.Text.

C# using System.Linq error

Why might "using System.Linq" cause the following error?
The type or namespace name 'Linq' does
not exist in the namespace 'System'
Reference System.Core
And then there are others that merge this namespace too - but that's the primary one on .Net 3.5 and above.
If you're project is currently .Net 2.0, say, and you're using the right version of VS (2005 and above) - you can simply right-click on the proejct properties; and change the 'Target Framework Version' to 3.5. System.Core will then become available.
If you don't see that in the options - then I guess you're using an older VS
The most probable reason is that you are using wrong version of .NET Framework.
Try to add System.Core assembly to your project
You'll get this error if you don't have "System.Core.dll" referenced (the assembly which contains the core LINQ APIs).
System.Linq is available in .Net 3.5 and above version.
Maybe you're targeting an older framework, Linq came in with 3.5 IIRC.
You are using lower version of .NET Framework than 3.5 to compile the source code or you don't have added the System.Core assembly to your project.
Manually type using System.Linq in the starting of the project, you will not be able to find this namespace in add reference dialogue box.
If you are still getting error then try to Add Reference System.Core.
If you are getting an error that it has been already referred then you can unload your project and then edit your csproject file, manually copy reference to System tag and paste and change the name to System.Core and reload the project.
In my case the only thing that worked was:
Adding a new Razor item (e.g. MVC 5 View Page)
That automatically pulls in some NuGet packages
The package that makes System.Linq available to Razor Views IntelliSense seems to be Microsoft.AspNet.WebPages.

Namespace in Referenced Project Present in Autocomplete Before Building, but Causes Compile Error After Building

I have a class library project which uses a namespace (e.g., "Cosmos.Creator.Util"). I then create a solution and windows forms application to test the library. From the windows form application, I add a reference to the library. So now I have two projects open in visual studio, a class library and a windows forms project. The forms project references the library.
When I edit my form's code, code autocompletion works correctly for the namespace that I use in the library. E.g., if I type "using Cosmos." I get autocomplete options like "Creator". But now if I build my solution, all of the "Cosmos" are red-underlined with the compile error: "The type or namespace name "Cosmos" could not be found (are you missing a using directive or an assembly reference?)".
For the purposes of the form application test, I placed my library code into a folder CosmosFormExample\Cosmos. When I check the reference from the form application, the reference is to CosmosFormExample\Cosmos\bin\Debug\Cosmos.dll, so that looks okay. I looked at the GUID referenced in the solution file and it matches the GUID of the project file Cosmos.csproj.
What has happened? How has the build caused my forms application to forget about the Cosmos namespace, despite the fact that it is still referencing the library project? Thanks much in advance.
Are you using VS2010 & .NET 4? If so you're probably using .NET 4 Client Profile instead of full fledged .NET 4. Go to project properties and check your Target Framework.
for more info: http://msdn.microsoft.com/en-us/library/cc656912.aspx
you need to check the framework you are using and the framework yout library was compiled for...

Categories

Resources