Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are @Scripts and @Styles commands not be recognized in MVC4 Razor layout file?

The following MVC4 Razor layout file loads several script and css bundles created in Bundle.config.

<!DOCTYPE html> <html class="no-js" lang="en"> <head>   <meta charset="utf-8" />   <title>XXX Beta</title>   @Scripts.Render(   "~/bundles/bundle_jquery",   "~/bundles/bundle_modernizr",   "~/bundles/bundle_foundation",   "~/bundles/bundle_jhs")   @Styles.Render(   "~/Content/bundle_foundation",   "~/Content/bundle_jhs")   @RenderSection("pageHeadContent", false) </head> <body>   <div id="bodyContainer">     @Html.Partial("Partial/_header")     <div id="contentContainer">       <div id="mainContentContainer">         <div id="sidebarContainer">           @RenderSection("sidebarContent", required: true)         </div>         @RenderBody()       </div>       <div class="clearBoth">       </div>     </div>     @Html.Partial("Partial/_footer")   </div> </body> </html> 

When the page is rendered the following error occurs. For some reason the @Scripts and @Styles commands are not being recognized. If I type in "@Scripts" in the files the Intellisense does not display/show the command. The project does reference System.Web.Optimization, which is used in the Bundle.config.

What could be causing the @Scripts and @Styles commands not to be recognized?


Server Error in '/' Application.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0103: The name 'Scripts' does not exist in the current context

Source Error:

Line 4: Line 5: XXX Beta Line 6: @Scripts.Render( Line 7: "~/bundles/bundle_jquery", Line 8: "~/bundles/bundle_modernizr",

Source File: c:\Users\username\Documents\Visual Studio 2010\Projects\XXX\Solution\xxx.website\Views\Shared_sidebarLayout.cshtml Line: 6

Show Detailed Compiler Output:

Show Complete Compilation Source:

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

like image 305
ChrisP Avatar asked Feb 02 '13 19:02

ChrisP


1 Answers

Make sure that the System.Web.Optimization.dll assembly has been referenced in your project and that the <add namespace="System.Web.Optimization"/> namespace has been added to your ~/Views/web.config file (not ~/web.config file):

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

The System.Web.Optimization.dll assembly is contained within the Microsoft.AspNet.Web.Optimization NuGet, so make sure that it is installed in your project (when you create a new ASP.NET MVC 4 project in Visual Studio using the Internet Template this NuGet is automatically added to the project).

like image 110
Darin Dimitrov Avatar answered Sep 19 '22 08:09

Darin Dimitrov