Visual Studio MVC 5 shows errors but compiles and runs okay

11,124

Solution 1

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.

Solution 2

Looks like VS *.cshtml editor marks the error erronously (although the compiler can resolve the reference).

To get rid of it, just include the System.Web.Optimization namespace at the top of your _Layout.cshtml like this: @using System.Web.Optimization. This should do the trick.

HTH Thomas

Share:
11,124

Related videos on Youtube

Starfleet Security
Author by

Starfleet Security

Updated on September 15, 2022

Comments

  • Starfleet Security
    Starfleet Security over 1 year

    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:

    @System.Web.Optimization.Styles.Render("~/Content/kendo/css")
    

    instead of:

    @Styles.Render("~/Content/kendo/css")
    

    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.

  • Silvermind
    Silvermind over 10 years
    Although that might work it shouldn't be necessary. That's only hiding the problem while there could be some deeper issue at hand here.
  • Starfleet Security
    Starfleet Security over 10 years
    Yes, doing that works (just as it would for a normal "class" file as I had mentioned), and honestly I didn't even realize you could do that in a *.cshtml file, but I agree with Silvermind, I shouldn't have to do that (hence why I asked the question to begin with). :)
  • Thomas Weller
    Thomas Weller over 10 years
    That's true - theoretically. But if you have no other issues then I wouldn't try to chase this little hickup any further. I don't think that it would pay. You 'solved' it with completely legal syntax, there's no ugly hack. So what? It's better to be practical on this...
  • Starfleet Security
    Starfleet Security over 10 years
    I would agree, but this is just a "pet project" of mine at home, so I'm willing to take the time to fix this and try to understand why it's happening, especially if it might help me in the future in a more "professional" situation.
  • Tommy
    Tommy over 10 years
    Just as a word of caution, the official migration document includes a few more pieces than just the web.config changes. Though it works in this case, given the amount of variety in project configurations, I recommend everyone to follow or at least review the official step by step - asp.net/mvc/tutorials/mvc-5/….