I’m working on an ASP.NET application in Visual Studio 2019 using NET 5.0 and attempting to display database entries using a webgrid that i have named ‘grid’ but am unable to use ‘grid.GetHtml’. Whenever I use it I receive the following error:
Reference to type ‘IHtmlString’ claims it is defined in ‘System.Web’, but it could not be found.
I have tried reinstalling all my NuGet packages and rebuilding my solution multiple times but no luck.
I feel like there may be some inconsistencies between my target framework and my packages but I’m not sure how to fix this. I had to create a web.config file to establish my connection string to the DB but other than that my project is pretty bare bones and straight forward so far.
[Code with ‘GetHtml’ error] [1]: https://i.stack.imgur.com/BvfJ7.png [List of NuGet Packages] [2]: https://i.stack.imgur.com/nkqf8.png
2
Answers
I found the solution for my particular issue. When creating the project in Visual Studio 2019 I selected the template 'ASP.NET Web App (Model View Controller)'. Apparently this template is not compatible with .NET 4.x, which is what some of the packages I was using required. In fact, I couldn't even find a way to switch the targeted framework to something besides 5.0 or 3.1.
So I created a new project from scratch using the template 'ASP.NET Web Application (.NET Framework)' which allowed me to select frameworks from a larger list, including the .NET 4.x frameworks (I used 4.8, this included additional files by default like the web.config file) which was necessary for my project.
Add the following to your file
Check here also: The type 'System.Web.IHtmlString' is defined in an assembly that is not referenced