名称“ViewBag”当前上下文中不存在
我正在尝试在我的应用程序中使用 ViewBag,我拥有所有最近的 dll、最新版本的 MVC 3,但我仍然收到错误:
“名称‘ViewBag’在当前上下文中不存在”
我什至卸载并重新安装了 MVC 3,但没有任何变化。
另外,我不相信 dll 会出现在 GAC 中。
我的问题可能是什么?或者如何将 dll 添加到 GAC 中?
I am trying to use ViewBag in my application, I have all of the recent dlls, the latest version of MVC 3, but yet I am still getting the Error:
"The name 'ViewBag' does not exist in the current context"
I have even uninstalled and then re-installed MVC 3 and yet there is no change.
Also, I do not believe that the dll's are showing up in the GAC.
What might be my problem? Or how to add the dll's to the GAC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
我也遇到了同样的问题。结果我丢失了
./Views/Web.config
文件,因为我从空的 ASP.NET 应用程序而不是使用 ASP.NET MVC 模板创建了该项目。对于 ASP.NET MVC 5,普通的
./Views/Web.config
文件包含以下内容:添加包含此内容的
./Views/Web.config
文件修复了此问题对我来说有问题。I was having the same problem. Turned out I was missing the
./Views/Web.config
file, because I created the project from an empty ASP.NET application instead of using an ASP.NET MVC template.For ASP.NET MVC 5, a vanilla
./Views/Web.config
file contains the following:Adding a
./Views/Web.config
file containing this content fixed this problem for me.您需要将 MVC 特定的 Razor 配置添加到 web.config 中。请参阅此处:未找到 Razor HtmlHelper 扩展(或视图的其他命名空间)
使用 MVC 3升级工具自动确保您拥有正确的配置值。
You need to add the MVC-specific Razor configuration to your web.config. See here: Razor HtmlHelper Extensions (or other namespaces for views) Not Found
Use the MVC 3 upgrade tool to automatically ensure you have the right config values.
尝试清理并重建。它对我来说有效。
Try to Clean and rebuild. It worked in my case.
我在 Visual Studio 2015 中升级到 MVC 5 的解决方案中遇到了同样的问题。
在 Views 文件夹中的 web.config 文件(不是根 web.config)中,我更新了
中引用的版本号
从2.0.0.0
到3.0.0.0
。I had the same problem in a solution that had been upgraded to MVC 5 in Visual Studio 2015.
In the web.config file within the Views folder (not the root web.config), I updated the version number referred to in
<configSections>
from2.0.0.0
to3.0.0.0
.尝试了不同的方法后,结果发现是 VS 缓存。您可以通过删除位于以下位置的缓存文件来解决该问题:
我关闭了我的项目,删除了该路径上的文件并重新打开我的项目,清理了解决方案并再次构建它,问题就解决了
当您下次启动 Visual Studio 时,将重新创建这些文件
After trying different things, it turns out it was VS cache. You can resolve it by deleting the cache files located in:
I closed my project, deleted the files on that path and reopened my project, cleaned the solution and built it again and the problem was solved
The files will be recreated when you next launch Visual Studio
我遇到了同样的问题,crimbo 给了我正确的线索,它是由 ./Views/Web.config 文件引起的,该文件存在但不包含正确的命名空间,我猜...
我创建了一个空白的 MVC5 项目并导入了它的 ./Views/Web.config 文件。 /Views/Web.config 到我现有的项目中,每次使用 ViewBag 下的红色波浪都消失了!
I had the same problem and crimbo gave me the right clue, it was caused by the ./Views/Web.config file which was present but not containing the right namespaces I guess...
I created a blank MVC5 project and imported its ./Views/Web.config into my existing project and the red waves under every ViewBag use are gone !
如果您使用 Visual Studio 2013 并且喜欢使用 MVC 3,则会收到此错误,因为 Visual Studio 2013 本身不支持 MVC 3(即使您更改 ./Views/web.config),也仅支持 MVC 4: https://msdn.microsoft.com/en-us/library/hh266747.aspx
If you use Visual Studio 2013 and you like use MVC 3, you get this error because Visual Studio 2013 does not support MVC 3 natively (even of you change ./Views/web.config), only MVC 4: https://msdn.microsoft.com/en-us/library/hh266747.aspx
我有一个
./Views/Web.Config
文件,但在发布网站后发生了此错误。结果文件上的构建操作属性设置为None
而不是Content
。将其更改为Content
可以使发布正常工作。I had a
./Views/Web.Config
file but this error happened after publishing the site. Turns out the build action property on the file was set toNone
instead ofContent
. Changing this toContent
allowed publishing to work correctly.在“属性”对话框中更改应用程序的默认命名空间后,我遇到了此问题。
./Views/Web.Config 包含对旧命名空间的引用
I had this problem after changing the Application's Default namespace in the Properties dialog.
The ./Views/Web.Config contained a reference to the old namespace
就我而言,将网页:版本更改为正确的值解决了我的问题,对我来说正确的值是(2.0.0.0 而不是 3.0.0.0) :
In my case, changing the webpage:Version to the proper value resolved my issue, for me the correct value was(2.0.0.0 instead of 3.0.0.0) :
在尝试了所有方法之后,没有一个对我有用,因为我所拥有的只是正确的配置。最后
从系统中删除“temp”和“%temp%”中的所有文件有助于解决此问题。
打开运行命令(Windows+R)并输入上面的字符串并删除所有临时文件。
After trying all approaches, none of them worked for me since all I have was correct configurations. finally
Deleting all files from "temp" and "%temp%" from system helped to resolve this issue.
Open Run command(Windows+R) and type above strings and delete all temporary files.
如果您已尝试了所有可用的答案但仍然找不到答案,这可能会解决问题。
如果您有不同的解决方案配置,例如调试、发布等
然后将项目输出路径设置为“bin”并编译项目。编译后恢复更改。
VS 在 bin 文件夹中查找 dll
If you had tried all available answers and still cannot find the answer this might solve issue.
If you have different solutions configurations like Debug, Release etc
then set project output path to 'bin' and compile project. Revert change after compiling.
VS looks for dlls in bin folder
我已经尝试删除 bin 和 obj 文件并重新启动 VS 但没有成功。
我也遇到过很多次这个问题,每次解决起来都很痛苦。通常这是由于 web.config 文件没有正确版本的引用之一。这意味着单击 Visual Studio 中的引用以查看属性选项卡中的版本,然后将其与 web.config 文件中的版本进行匹配。
另一种方法是(如果可能)升级到更高版本的 .net 框架,然后删除 bin/obj 文件并重新启动 Visual Studio。我只能假设它正在更改中的某些内容,
快速检查 csproj 文件之间的差异实际上并没有显示任何重大差异...但它确实显示的差异是(我添加(删除)以显示旧行)
在 Web.Config 文件(不是 View 中的文件)中
,它还添加了(到同一个 web.config 文件),但我手动删除了它
最后,在包管理器控制台中,添加
update-package
在本地运行网站并查看我的第二段修复的任何编译错误(与参考文献的版本匹配)
I had already tried deleting the bin and obj file and restarting VS and had no luck.
I've also had this issue many times and it's a pain to solve each time. Often it is due to the web.config file not having the correct version of one of the references. This means click on the reference in Visual Studio to see the version in the property tab, and then match it to the version in the web.config files.
Another way is (if possible) upgrade to a later version of the .net framework and then deleting bin/obj files and restarting Visual Studio. I can only assume it's changing something in the
A quick check of the diff between the csproj file doesn't actually show any major difference... But the differences it did show was (I've added (remove) to show the old line)
In the Web.Config file (not the one in View)
It also added (to the same web.config file) but I manually removed it
Finally, in the Package Manager Console, add
update-package
Run the website locally and see any compilation errors which was fixed by my second paragraph (matching the versions of the references)
我更新了网页:./Views/Web.Config 文件夹中的版本,但此设置也存在于根目录中的 web.config 中。更新两者或从根 web.config 中删除
I updated webpages:Version under in ./Views/Web.Config folder but this setting was also present in web.config in root. Update both or remove from root web.config
正如@Wilson Vallecilla 已经提到的。请执行以下步骤删除缓存:
请按照以下路径查找文件:
我关闭了我的项目,删除了该项目上的文件路径并重新打开我的项目,清理解决方案并再次构建它,问题得到解决
删除临时 ASP.NET 文件也有帮助。 C:\Users\your.name.here\AppData\Local\Temp\Temporary ASP.NET Files。
这对我有用。
谢谢!
As @Wilson Vallecilla already mentioned. Please do the below steps to delete the cache:
Please follow below path to discover the files:
I closed my project, deleted the files on that path and reopened my project, cleaned the solution and built it again and the problem was solved
Deleting your Temporary ASP.NET Files also helps. C:\Users\your.name.here\AppData\Local\Temp\Temporary ASP.NET Files.
This works for me.
Thanks!
对于 MVC5,如果您从头开始构建应用程序。您需要将 web.config 文件添加到 Views 文件夹中,并将以下代码粘贴到其中。
请注意,对于 MVC 3,您必须将版本更改为 3.0.0.0,
您可能需要关闭并再次打开 *.cshtml 页面才能看到更改。
For MVC5, in case you are building an application from scratch. You need to add a web.config file to the Views folder and paste the following code in it.
Note that for MVC 3 you will have to change version to 3.0.0.0 at
You may have to close and open the *.cshtml page again to see the changes.