Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 3 asking for System.Web version 2.0.0.0 (System.Web.UI.Controls): Is this a compiler bug?

Setup:

An MVC 3 app I am working on suddenly won't compile. I get the following error:

The type 'System.Web.UI.Control' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

This is clearly absurd: I have System.Web version 4.0.0.0 on my machine.

If I remove the code at the relevant line, the error simply moves to the next line.

Visual Studio had been complaining about an add in, eg, if I was trying to Go To Definition, VS would complain and ask if I wanted to remove the add in.

Questions:

Is VS mangled?

Do I need to reinstall the whole bally thing (VS 2010, power tools, MVC 3 and heck knows what else)?

EDIT:

Since finding the cause and therefore solving the issue, I have renamed the question to a name that is more likely to be found by others with the same or similar problem.

To the best of my abilities, my answer tells how to troubleshoot this issue. But if anyone can come up with a full explanation for this issue, then I will gladly mark that as the answer.

like image 963
awrigley Avatar asked Dec 08 '25 03:12

awrigley


1 Answers

OK, I have got to the bottom of this.

The problem was that I was using some string extensions. These were packed in a library, written ages ago.

The issue is caused by the namespace. Lets call it WS.Extensions

In WS.Extensions I also had some extension methods for plain vanilla asp.net server controls, and the classes these extensions are in have using statements for System.Web.UI.

Although the relevant dlls are referenced in the library project, it seems that this causes issues with MVC 3 and, to my mind, an obscure error. Obvious once you know where the issue lies, but not otherwise.

I have moved all the extensions to a new library with no cross using statements that reference server control namespaces such as System.Web.UI.

I have not deleted my post as I think that the problem could happen to others moving from the horrors of server controls to MVC.

Addendum:

The nasty thing of this error is that it shows as a compiler error, with a specific line where the "problem" is. Commenting out this line just moves the line that the compiler complains about down one line. Also, the line had absolutely NOTHING to do with the lines where the extensions were used. So there were no clues.

As a result, I would consider this to be a compiler bug, that has nothing to do with ASP.NET MVC, 3 or otherwise.

It is caused by referencing (using) a namespace from and ASP.NET MVC 3 app, when that namespace includes code that is written for ASP.NET server controls.

I lost half a day on the problem, so hope that this helps someone else. As a result, have renamed the question.

like image 161
awrigley Avatar answered Dec 10 '25 16:12

awrigley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!