93

我正在尝试在我的应用程序中使用 ViewBag,我拥有所有最新的 dll,最新版本的 MVC 3,但我仍然收到错误消息:

“当前上下文中不存在名称‘ViewBag’”

我什至卸载并重新安装了 MVC 3,但没有任何变化。

另外,我不相信 dll 会出现在 GAC 中。

我的问题可能是什么?或者如何将dll添加到GAC?

4

16 回答 16

85

我遇到了同样的问题。原来我丢失了该./Views/Web.config文件,因为我是从一个空的 ASP.NET 应用程序而不是使用 ASP.NET MVC 模板创建项目的。

对于 ASP.NET MVC 5,vanilla./Views/Web.config文件包含以下内容:

<?xml version="1.0"?>

<!-- https://stackoverflow.com/a/19899269/178082 -->
<configuration>
    <configSections>
        <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">
            <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
            <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
        </sectionGroup>
    </configSections>

    <system.web.webPages.razor>
        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <pages pageBaseType="System.Web.Mvc.WebViewPage">
            <namespaces>
                <add namespace="System.Web.Mvc" />
                <add namespace="System.Web.Mvc.Ajax" />
                <add namespace="System.Web.Mvc.Html" />
                <add namespace="System.Web.Routing" />
            </namespaces>
        </pages>
    </system.web.webPages.razor>

    <appSettings>
        <add key="webpages:Enabled" value="false" />
    </appSettings>

    <system.web>
        <httpHandlers>
            <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
        </httpHandlers>

        <!--
                Enabling request validation in view pages would cause validation to occur
                after the input has already been processed by the controller. By default
                MVC performs request validation before a controller processes the input.
                To change this behavior apply the ValidateInputAttribute to a
                controller or action.
        -->
        <pages
                validateRequest="false"
                pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
                pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
                userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <controls>
                <add assembly="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
            </controls>
        </pages>
    </system.web>

    <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />

        <handlers>
            <remove name="BlockViewHandler"/>
            <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
        </handlers>
    </system.webServer>
</configuration>

添加./Views/Web.config包含此内容的文件为我解决了这个问题。

于 2013-11-11T05:12:19.790 回答
39

您需要将特定于 MVC 的 Razor 配置添加到您的 web.config。请参阅此处:未找到 Razor HtmlHelper Extensions (or other namespaces for views)

使用MVC 3 升级工具自动确保您拥有正确的配置值。

于 2011-02-10T17:19:52.533 回答
19

尝试清理和重建。它在我的情况下有效。

于 2013-03-18T04:06:03.940 回答
9

我在 Visual Studio 2015 中升级到 MVC 5 的解决方案中遇到了同样的问题。

在 Views 文件夹(不是根 web.config)中的 web.config 文件中,我更新了<configSections>2.0.0.0到引用的版本号3.0.0.0

<configuration>
    <configSections>
      <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">
        <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
        <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      </sectionGroup>
  </configSections>
于 2015-08-31T14:56:23.683 回答
6

在尝试了不同的东西之后,原来是 VS 缓存。您可以通过删除位于以下位置的缓存文件来解决它:

C:\Users\your.name.here\AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache

我关闭了我的项目,删除了该路径上的文件并重新打开了我的项目,清理了解决方案并再次构建它,问题就解决了

下次启动 Visual Studio 时将重新创建文件

于 2016-02-11T08:59:22.963 回答
3

我遇到了同样的问题,crimbo 给了我正确的线索,这是由 ./Views/Web.config 文件引起的,该文件存在但不包含我猜的正确命名空间......

我创建了一个空白的 MVC5 项目并将其 ./Views/Web.config 导入到我现有的项目中,每次使用 ViewBag 下的红波都消失了!

于 2013-12-18T08:59:17.327 回答
2

如果您使用 Visual Studio 2013 并且喜欢使用 MVC 3,则会收到此错误,因为 Visual Studio 2013 本身不支持 MVC 3(即使您更改 ./Views/web.config),仅支持 MVC 4:https://msdn .microsoft.com/en-us/library/hh266747.aspx

于 2015-01-26T17:38:56.747 回答
2

我有一个./Views/Web.Config文件,但是在发布网站后发生了这个错误。原来文件上的构建操作属性被设置为None而不是Content. 将此更改为Content允许发布正常工作。

于 2015-02-24T20:05:07.627 回答
1

就我而言,将 pages:Version 更改为正确的值解决了我的问题,对我来说正确的值是 (2.0.0.0 而不是 3.0.0.0) :

<appSettings>
        <add key="webpages:Version" value="2.0.0.0"/>
        <add key="webpages:Enabled" value="false"/>
于 2016-03-21T20:31:36.573 回答
1

在尝试了所有方法之后,它们都不适合我,因为我所拥有的只是正确的配置。最后

从系统中删除“temp”和“%temp%”中的所有文件有助于解决此问题。

打开运行命令(Windows+R)并输入以上字符串并删除所有临时文件。

于 2016-07-19T18:47:43.067 回答
1

我已经尝试删除 bin 和 obj 文件并重新启动 VS,但没有运气。

我也遇到过很多次这个问题,每次解决都很痛苦。这通常是由于 web.config 文件没有引用之一的正确版本。这意味着单击 Visual Studio 中的引用以在属性选项卡中查看版本,然后将其与 web.config 文件中的版本进行匹配。

另一种方法是(如果可能)升级到更高版本的 .net 框架,然后删除 bin/obj 文件并重新启动 Visual Studio。我只能假设它正在改变

快速检查 csproj 文件之间的差异实际上并没有显示任何重大差异......但它确实显示的差异是(我添加(删除)以显示旧行)

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> (remove)
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>(remove)
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>

在 Web.Config 文件中(不是 View 中的那个)

<add key="webpages:Version" value="2.0.0.0" /> (remove)
<add key="webpages:Version" value="3.0.0.0"/>

它还添加了(到同一个 web.config 文件),但我手动删除了它

 <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>

最后,在 Package Manager Console 中,添加update-package

在本地运行网站并查看我的第二段修复的任何编译错误(匹配参考的版本)

于 2017-11-08T07:44:17.147 回答
1

在“属性”对话框中更改应用程序的默认命名空间后,我遇到了这个问题。

./Views/Web.Config 包含对旧命名空间的引用

于 2015-12-11T10:09:42.273 回答
1

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.

Project Output Path

VS looks for dlls in bin folder

于 2016-10-27T08:44:34.487 回答
0

对于 MVC5,如果您从头开始构建应用程序。您需要将 web.config 文件添加到 Views 文件夹并将以下代码粘贴到其中。

<?xml version="1.0"?>
<configuration>
  <configSections>
    <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">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
</configuration>

请注意,对于 MVC 3,您必须将版本更改为 3.0.0.0

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

您可能必须关闭并再次打开 *.cshtml 页面才能看到更改。

于 2019-09-05T11:56:38.460 回答
0

正如@Wilson Vallecilla 已经提到的那样。请执行以下步骤来删除缓存:

请按照以下路径查找文件:

C:\Users\your.name.here\AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache

删除所有四个文件:

  • Microsoft.VisualStudio.Default.cache
  • Microsoft.VisualStudio.Default.catalogs
  • Microsoft.VisualStudio.Default.err
  • Microsoft.VisualStudio.Default.external

我关闭了我的项目,删除了该路径上的文件并重新打开了我的项目,清理了解决方案并再次构建它,问题就解决了

删除临时 ASP.NET 文件也有帮助。C:\Users\your.name.here\AppData\Local\Temp\Temporary ASP.NET 文件。

这对我有用。

谢谢!

于 2019-02-12T10:41:47.237 回答
0

我更新了 ./Views/Web.Config 文件夹下的网页:版本,但此设置也存在于根目录的 web.config 中。更新两者或从根 web.config 中删除

于 2016-09-06T10:00:25.653 回答