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:
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. 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.
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)
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.
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 !!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With