我只想澄清一下我尝试过的解决方案:
- Make sure
Microsoft.AspNet.Web.Optimization
orSystem.Web.Optimization
is there. I haveSystem.Web.Optimization
in References folder. I tried to installMicrosoft.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 rootWeb.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.