Starfleet Security Starfleet Security - 1 year ago 105
C# Question

Visual Studio MVC 5 shows errors but compiles and runs okay

I'm getting a rather strange error, which seems to have started when I updated several NUGET packages (including to MVC 5). In my "_Layout.cshtml" file, I now get the error messages that you can see in this picture (sorry, I'm too new to post it directly here yet).

See here for error

These are listed as errors, not warnings. Yet my build succeeds and the project runs, and as far as I can tell it works fine. I only see these errors when I have the "_Layout.cshtml" file open. It's acting as though it can't see "System.Web.Optimization". However, if I hover over the line, it will prompt me to insert that text and make it say:


instead of:


But I never had to explicitly spell this out before, and if I start a new project it doesn't require me to do so. Obviously, some part of my updates has caused this strange behavior (I assume it's related to a config setting somewhere, but I can't figure out what or where). I've checked both config files (the "project" one and the "views" one), and I'm pretty sure the issue must be there, but it's beyond my ability to see the problem. If this were a normal "class" file, it would be obvious that I was missing a "using" statement at the top, but that doesn't seem to apply here.

Can anyone point me to a solution, other than explicitly pointing to the full reference in the code as mentioned above? That just doesn't seem like it should be necessary, and I feel like I'd be skirting the issue by doing so.

Answer Source

The most likely cause of this issue is that the web.config in your Views folder(s) is/are broken. Upgrading the ASP.NET MVC 5 requires quite a few configuration changes that I am not certain the NuGet package manager does (or does well, I think it takes care of a few of these). Along with the MVC 5 .dll, many of the other related assemblies also need to be updated and the related references updated as well.

Take a look at the following tutorial and ensure you have completed EACH of the required steps. Then do a clean, rebuild and see if your issues are resolved.

I have found that it is sometimes better to have VS create a new MVC project, then look at and compare the web.configs (both application root as well as the views folder web.config) that it generates to your project's configuration files to ensure that you are not including namespaces that don't belong and all version numbers are correct.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download