I'm starting to develop in ASP.NET again and I ran into a small error within Visual Studio. My .cshtml files show errors when using a few razor functions. For example "The name 'ViewBag' does not exist in the current context". Here is a picture:
I am using a demo project. You can find the project here: https://github.com/Wintellect/Angular-MVC-Cookbook/tree/master/BasicProject
I have looked through several other threads and most suggest to update the web.config file(s). These 2 config files are already present and since it's a pretty popular demo I assume it has all the required configuration in it. I have of course looked through these config files and they do indeed include the suggested solutions.
Other details:
- I have already used clean & rebuild on the solution but that changed nothing.
- When I create a completely new MVC project it does work
- My friend has the same problem and we both use VS 2015 and Windows 10
- I can still run the application and it does work.
Thanks in advance.
late to the game but none of these solutions worked for me.
I upgraded to VS 2017, and the site worked, but Visual Studio compiler broke most of .cshtml files
TLDR;
mysolution.sln had the wrong version
so you want to create a new solution with newer VS and compare it to yours and copy version info over.
full upgrade steps:
i faced this issue more times and finally i discovered that it happened because the folder of views should contain it is own web config
I had added the controller by
corresponding code created:
My errors got changed by changing the Step 1 as:
corresponding code created:
The problem was: I had not Inherited my class from Controller
Other messages in my error included - all of which were from this problem:
Hope this change helps those who have not paid attention in their initial steps.
Update the version numbers in the settings of the
web.config
file in the Views folder.If you have updated the MVC version through nuget, should be:
Other dependency versions might need to be updated too. Compile and namespace issues in Views for MVC helpers and objects are typically related to messed up
web.config
files within the View folders.I've wasted well over a working day on this. I tried everything in this (and other) posts, to no avail. Eventually I found that I needed to open the project in a different way. I've just upgraded an existing website to Visual Studio 2015, MVC5, .NET framework 4.5.2 and am using Windows 10, but suspect the answer would have been the same for other versions of each software application.
So this is the menu option I was choosing to open a website:
Instead of this, I opened the csproj file defining the project using this menu option:
I then had the "clean solution" option many other people refer to:
However, I didn't even need this - everything just worked! Well, I got on to the next set of problems, anyway ...
Turning it off and on again worked for me. You can try restarting visual studio.