I have just created an ASP.NET MVC 4 project and used Visual Studio 2012 RC to create a Controller and Razor Views for Index
and Create
Actions.
When I came to run the application, and browsed to the Create
view, the following error was shown:
Compiler Error Message: CS0103: The name 'Scripts' does not exist in the current context
The problem is the following code which was added automatically to the bottom of the View:
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Why does Scripts not exist?
I looked at the base Web Page class in Assembly System.Web.Mvc.dll, v4.0.0.0
I can see the following helper properties available:
- Ajax
- Html
- Url
But nothing named Scripts.
Any ideas?
EDIT:
My Web.config file looks like this (untouched from the one that Visual Studio created):
<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.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
EDIT #2:
People are blogging about using the @Scripts
helper:
Yet having just installed Visual Studio 2012 RC onto a fresh Windows 8 install I am still unable to use @Scripts
even though Visual Studio adds it to the generated View!
Solutions are presented below.
I am not sure how to close this, because in the end an update seemed to resolve the issue. I double checked I had performed a clean install, using a new project. But the same failing project I had made works fine now after various updates and no manual obvious intervention. Thanks for all of the thoughts but there was definitely an issue at the time ;)
@Styles
and@Scripts
are 2 new helpers provided bySystem.Web.Optimization
library. As the name suggests, they bundle and minify CSS and JavaScript files or resources respectively.Try including the namespace
System.Web.Optimization
either by@using
directive or throughweb.config
http://ofps.oreilly.com/titles/9781449320317/ch_ClientOptimization.html#BundlingAndMinificationUPDATE
Microsoft has moved the bundling/minification to a separate package called
Microsoft.AspNet.Web.Optimization
. You can download the assembly from nuget.This post will be useful to you.
I had the same issue:
The System.Web.Optimization version I was using was outdated for MVC4 RC.
I updated my packages using the package manager in VS 2010.
In this MSDN blog, Mr. Andy talks about how to update your MVC 4 Beta project to MVC 4 RC. Updating my packages got the Scripts (particularly the web optimization one) to resolve for me:
To install the latest System.Web.Optimization package, use Package Manager Console (Tools -> Library Package Manager -> Package Manager Console) and run the below command:
Use the System.Web.Optimization file included in the package in your references.
To update other packages: Tools menu -> Library Package Manager -> Manage NuGet Packages for Solution.
I had this issue after I added an Area to a project that didn't have any. To get rid of it just copied the web.config withing root Views folder to the Views folder of the area and it started working.
The key here is to add
to BOTH web.config files. My scenario was that I had System.Web.Optimization reference in both project and the main/root web.config but @Scripts still didn't work properly. You need to add the namespace reference to the Views web.config file to make it work.
UPDATE:
Since the release of MVC 4 System.Web.Optimization is now obsolete. If you're starting with a blank solution you will need to install the following nuget package:
You will still need to reference System.Web.Optimization in your web.config files. For more information see this topic:
How to add reference to System.Web.Optimization for MVC-3-converted-to-4 app
As many pointed out, restart of VS could be required after the above steps to make this work.
If you added to your web.config and it still shows message, then you need to close your project and reopen it, now it will exist and @Styles.Render("") and @Scripts.Render() will work fine.
I had the same problem and I used WinMerge to help me track this down. But as I researched it more, I found that Rick has the perfect blog post for it.
Summary:
<add namespace="System.Web.Optimization"/>
to both web.config filesInstall-Package -IncludePrerelease Microsoft.AspNet.Web.Optimization