@ViewBag does not exist on current context

▼魔方 西西 提交于 2020-08-23 07:02:10

问题


I understand there are some solutions online already but I am confused by myself after trying all the solutions.

I just want to clarify the solutions which I tried:

  1. Make sure Microsoft.AspNet.Web.Optimization or System.Web.Optimization is there. I have System.Web.Optimization in References folder. I tried to install Microsoft.AspNet.Web.Optimization in Nuget and the console told me it already exists. I think this means I have it in references already.
  2. Add the namespace to ~/Views/Web.config(not root Web.config). In my ~/Views/Web.config I have:

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

    and in my root Web.config:

    <system.web>
     <machineKey configSource="Config\MachineKey.config"/>
     <httpRuntime targetFramework="4.5" maxRequestLength="20480"/>
     <globalization culture="en-CA" uiCulture="en-CA"/>
     <customErrors mode="Off"/>
     <compilation debug="true" targetFramework="4.5">
      <assemblies>
       <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, 
        PublicKeyToken=b77a5c561934e089"/>
       <add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, 
        PublicKeyToken=31BF3856AD364E35"/>
      </assemblies>
     </compilation>
     <pages buffer="true" enableViewState="false">
      <namespaces>
       <add namespace="System.Web.Helpers"/>
       <add namespace="System.Web.Mvc"/>
       <add namespace="System.Web.Mvc.Ajax"/>
       <add namespace="System.Web.Mvc.Html"/>
       <add namespace="System.Web.Optimization"/>
       <add namespace="System.Web.Routing"/>
       <add namespace="System.Web.WebPages"/>
      </namespaces>
     </pages>
    </system.web>
    

    I searched online and one of the answers told me the namespaces should be the same. I did but nothing changes to me.

  3. Make sure the target version. My target framework is 4.5. My MVC version is 5.2.0.0. Actually I am not understand this solution. I tried to change the framework version to 4.5.1 and the visual studio told me my version is actually 4.5. I guess my target version is OK....(Actually I do not understand this solution)

  4. Add @using System.Web.Optimization to ~/Views/Shared/_Layout.cshtml. This made something happens. My @Scripts.Render is OK now but @ViewBag still has a red line under it.

  5. Delete ComponentModelCache and restart Visual Studio. I am using VS 2015. I tried, nothing happens.

I will perform these solutions more precisely later but I just want to know if there are other solutions I missed and worth trying. Thank you so much for the help !!


回答1:


After a few hours digging. I solved my issue. My solution 1 can only remove the RED LINEs and it gave me 500 error When I run Localhost debug.

Solution 1: I simply changed the version from 3.0.0.0 to 2.0.0.0 in below code of ~/Views/Web.config:

  <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>

When I changed all the versions to 2.0.0.0, the red lines went away but I have 500 error. Which means system.web.webPages.razor loaded to the _Layout.cshtml correctly.

To my understanding, My below settings of ~/Views/Web.config did not load if the system.web.webPages.razor version is incorrect:

  <system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.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" />
    <add namespace="System.Web.Optimization" />
  </namespaces>
</pages>
</system.web.webPages.razor>

Solution 2: Then, I checked below part of code in root Web.config:

 <connectionStrings configSource="Config\ConnectionStrings-dev.config" />
<appSettings>
  <add key="webpages:Version" value="2.0.0.0" />
  <add key="webpages:Enabled" value="false" />
  <add key="PreserveLoginUrl" value="true" />
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

the webpages:version is 2.0.0.0. I changed it to 3.0.0.0 to match the version in ~/Views/Web.config, restart visual studio, it worked without any issue.

This link is the one helped me.




回答2:


Take care in your project file a razor assembly is loaded, for me it was the version 2:

    <Reference Include="System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
  <Private>True</Private>
  <HintPath>..\packages\Microsoft.AspNet.Razor.2.0.20715.0\lib\net40\System.Web.Razor.dll</HintPath>
</Reference>

In the /Views/Web.config an other version was pointed, the version 3:

    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
  <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>

And the load of the version 3 fails when the VS cshtml editor tried to load it. All razor references appear than unknown (ViewBag, @Html, etc.) Finally when I link the same version all runs fine inside cshtml editor ;)



来源:https://stackoverflow.com/questions/52748122/viewbag-does-not-exist-on-current-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!