可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
I had this issue despite having all the correct configuration.
It turned out to be some bad files in the Component Cache, preventing the Razor views from recognising ViewBag, Model, and HtmlHelpers. Deleting these files solved the problem (good versions of these files were created next time I opened Visual Studio).
The files are located here:
%LOCALAPPDATA%\Microsoft\VisualStudio\14.0\ComponentModelCache
Delete all four files:
- Microsoft.VisualStudio.Default.cache
- Microsoft.VisualStudio.Default.catalogs
- Microsoft.VisualStudio.Default.err
- Microsoft.VisualStudio.Default.external
I have subsequently seen the same issue on several other developer machines and this fix quickly solves it.
回答2:
Update the version numbers in the settings of the web.config
file in the Views folder.
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
If you have updated the MVC version through nuget, should be:
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
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.
回答3:
Find "webpages:Version" in the appsettings and update it to version 3.0.0.0. My web.config had
<add key="webpages:Version" value="2.0.0.0" />
and I updated it to
<add key=”webpages:Version” value=”3.0.0.0″ />
回答4:
After trying everything under the sun, it turns out something had modified my System.Web.WebPages.Razor
<sectionGroup>
in /Views/Web.Config
from the proper CamelCase System.Web.WebPages.Razor
to an all-lowercase system.web.webpages.razor
which ultimately was my demise.
Hoping this may help some other poor soul with this problem...
For reference, this is the correct entry (for my scenario)
<sectionGroup name="System.Web.WebPages.Razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
回答5:
I accidentally removed web.config
from View folder, thinking that it was unnecessary. When I put it back it started working.
回答6:
Sometimes it is not enough to change version numbers in the settings of the web.config. This problem occurs because Visual Studio 2015 doesn't have mvc4 tooling.
Solution is to upgrade your project to MVC 5. It totally solves the problem.
You can get help from this page. It clearly explains it.
http://www.asp.net/mvc/overview/releases/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2
By the way Visual Studio team declared they will add mvc4 support to Visual Studio 2015 with the first update.
回答7:
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
回答8:
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 ...
回答9:
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:
- ensure .sln file has correct version for VS
- right click MySolution in VS > Nuget Manager > Updates > run all updates
- right click MySolution in VS > Application > update Target Framework (4.5.2 in my case)
- fix any version issues in .config files (i.e. RAZOR to 3.0.0.0)
- fix any code issues in compiler (for me ForEach was ambiguous)
回答10:
I was able to resolve the issue by adding the following to the top of my .cshtml page
@{ViewBag.Title = "Title";}
After recompiling I was able to delete this line and the error was gone.
回答11:
After trying everything else and feeling a bit frustrated, I upgraded the .NET framework on my MVC5 app from 4.5 to 4.5.2. Somehow it fixed my problem. Hope it helps.
回答12:
Turning it off and on again worked for me.
You can try restarting visual studio.
回答13:
I had added the controller by
- Step 1: Add > Class
- Step 2: UserController
- .
- .
- .
corresponding code created:
namespace SampleApp.Controllers
{
public class UserController {
}
}
My errors got changed by changing the Step 1 as:
- Step 1: Add > Controller
- Step 2: User Controller
- .
- .
- .
corresponding code created:
namespace SampleApp.Controller {
public class UserController : Controller {
}
}
The problem was:
I had not Inherited my class from Controller
Other messages in my error included - all of which were from this problem:
error CS0117: 'ModelState' does not contain a definition for 'IsValid'
error CS0103: The name 'RedirectToAction' does not exist in the
current context
error CS0103: The name 'View' does not exist in the current context
error CS0103: The name 'ViewBag' does not exist in the current context
error CS0103: The name 'Json' does not exist in the current context
Hope this change helps those who have not paid attention in their initial steps.
回答14:
I solved replacing in web.config
<add key="webpages:Enabled" value="false" />
with
<add key="webpages:Enabled" value="true" />