我想在我的应用程序中使用ViewBag,我把所有的最近的dll,MVC 3的最新版本,但尚未我仍然得到错误:
“‘ViewBag’这个名字不会在目前情况下存在”
我甚至已经卸载,然后重新安装MVC 3,但没有任何改变。
另外,我不相信DLL的都出现在GAC了。
什么可能是我的问题吗?或者如何添加DLL的到GAC?
您需要将特定的MVC-剃刀配置添加到您的web.config。在这里看到:Razor HtmlHelper Extensions (or other namespaces for views) Not Found
使用MVC 3 upgrade tool自动确保你有合适的配置值。
在我的情况下,改变网页:版本为适当的值解决了我的问题,对我来说是正确的值(2.0.0.0的,而不是3.0.0.0):
<appSettings>
<add key="webpages:Version" value="2.0.0.0"/>
<add key="webpages:Enabled" value="false"/>
尝试所有方法后,他们没有工作对我来说,因为我的一切是正确的配置。最后
删除从“临时”和“%temp%”从系统中的所有文件,帮助解决了这个问题。
打开运行命令(的Windows + R)及以上的字符串类型,并删除所有临时文件。
我已经尝试删除bin和OBJ文件并重新启动VS和没有运气。
我也有这个问题很多次,它的每一次解决一个痛。往往是由于不具有引用的一个正确版本的web.config文件。这意味着点击Visual Studio中的参考,看看在属性选项卡的版本,然后将其与在web.config文件的版本。
另一种方式是(如果可能)升级到.NET Framework的更高版本,然后删除斌/ 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=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
最后,在包管理器控制台,添加update-package
本地运行的网站,看看哪些是固定我的第二段任何编译错误(匹配参考的版本)
在./Views/Web.Config文件夹下的版本,但此设置也存在于根web.config:我更新了网页。同时更新或从根web.config中删除
作为@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 \应用程序数据\本地的\ Temp \临时ASP.NET文件。
这对我的作品。
谢谢!
对于MVC5,如果你是从头开始构建的应用程序。你需要一个web.config文件添加到视图文件夹并粘贴到它下面的代码。
<?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页面看到的变化。
我有同样的问题。原来我错过了./Views/Web.config
文件,因为我从创建一个空的ASP.NET应用程序而不是使用ASP.NET MVC模板项目。
对于ASP.NET MVC 5,香草./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
文件解决了这个问题对我来说。
试图清理和重建。它的工作在我的情况。
我有这样的已经在Visual Studio 2015年升级到MVC 5解决同样的问题。
在视图文件夹(未根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>
尝试不同的事物后,事实证明这是VS的缓存。您可以通过删除在高速缓存中的文件解决它:
C:\ Users \用户your.name.here \应用程序数据\本地\微软\ VisualStudio的\ 14.0 \ ComponentModelCache
我闭上我的项目,删除该路径上的文件并重新打开我的项目,清理解决方案,并重新建造它,问题就解决了
该文件将被重新创建,当你下次启动Visual Studio的
我有同样的问题,crimbo给了我正确的线索,它是由这是存在的,但不包含正确的命名空间我想./Views/Web.config文件引起...
我创建了一个空白MVC5项目,其进口到./Views/Web.config我现有的项目,并在每次使用ViewBag红波都不见了!
如果您使用Visual Studio 2013,你喜欢使用MVC 3,你是因为的Visual Studio 2013不支持MVC 3本身(甚至你改变./Views/web.config),只有MVC 4得到这个错误:https://msdn.microsoft.com/en-us/library/hh266747.aspx
我有一个./Views/Web.Config
文件,但在发布站点之后发生这种错误。原来,生成操作属性的文件设置为None
而不是Content
。改变这Content
允许发布到正常工作。
我在属性对话框中改变应用程序的默认命名空间后有这个问题。
该./Views/Web.Config包含旧命名空间的引用