Search code examples
c#console-applicationvisual-studio-2022system.web

Trying to add dependency System.Web.UI to Console Application in Visual Studio Community 2022


I am using another piece of software that will be executing a C# Script.\

I am using VS2022 to code that script because the other software's IDE sucks. All I'm trying to do is run this code (to begin with):

using System.Web.UI.HtmlControls;
using (StringWriter sw = new StringWriter())
{
  Table t = new Table();
  TableRow tr = new TableRow();
  TableCell td = new TableCell {Text = "Some text... Istanbul"};

  tr.Cells.Add(td);
  t.Rows.Add(tr);

  t.RenderControl(new HtmlTextWriter(sw));

  string html = sw.ToString();
  Console.Write(html);
}

When I execute I get "UI doesn't exist in namespace System.Web".

???

So now I'm trying to add a reference, but I can't figure out how to do this. In Reference Manager there's nothing to chose from except COM stuff. I browse to "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Web.dll", but when I chose it, it says "The reference is invalid or unsupported."

What the heck am I supposed to do here?

Why is this so difficult. It didn't used to be in earlier versions on VS.


Solution

  • The issue you're encountering is due to the differences between .NET Framework and .NET Core. System.Web.UI.HtmlControls is part of the .NET Framework, but it is not supported in .NET Core or .NET. If you're using .NET 6 or a later version, you won't be able to reference System.Web.dll directly because it's not available in these newer frameworks. So you better create a .NET Framework project and try again.