202

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

5
  • already try add in Web.config? <system.web.webPages.razor> <pages pageBaseType=""> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> etc... </pages>
    – Zach dev
    Jun 8, 2012 at 16:26
  • I have added the section from Web.config, but I haven't changed anything in that file. Jun 8, 2012 at 16:41
  • 6
    You mean "My Views\Web.config" file not the app root web.config. At any rate, it's incorrect and missing <add namespace="System.Web.Optimization"/>. Did you install VS12 Beta prior to RC? What win8 version? Jun 16, 2012 at 0:46
  • @RickAndMSFT I did a clean Win8 installation from the Release Preview ISO, along with an install from the VS 2012 RC ISO, to make absolutely sure it wasn't a conflict of versions. Jun 28, 2012 at 23:15
  • @RickAndMSFT My Views\Web.config would miss any Views in Areas
    – Maslow
    Feb 25, 2013 at 15:27

24 Answers 24

270

The key here is to add

 <add namespace="System.Web.Optimization" /> 

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:

Install-Package Microsoft.AspNet.Web.Optimization

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.

6
  • 8
    You may also have to run Install-Package Microsoft.AspNet.Web.Optimization from a blank ASP.NET MVC4 template as it is not included by default.
    – Portman
    Jul 18, 2013 at 6:35
  • @Portman Yes indeed if you started with a blank MVC project template you need to install Microsoft.AspNet.Web.Optimization package.
    – Marko
    Jul 18, 2013 at 7:49
  • 2
    It only needs to be added in the web.config in the Views folder for me. Jul 29, 2013 at 12:03
  • 9
    This is indeed the solution. However, I needed to restart visual studio for this to be recognized. Feb 23, 2016 at 14:00
  • 4
    I had to add this to my Views/Web.config and then restart Visual Studio.
    – Caverman
    Sep 6, 2016 at 19:41
49

@Styles and @Scripts are 2 new helpers provided by System.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 through web.config

http://ofps.oreilly.com/titles/9781449320317/ch_ClientOptimization.html#BundlingAndMinification

UPDATE

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.

4
  • 2
    Adding System.Web.Optimization says "CS0234: The type or namespace name 'Optimization' does not exist in the namespace 'System.Web". There is no reference called System.Web.Optimization to add either. Jun 8, 2012 at 21:15
  • 1
    The System.Web.Optimization namespace has been made obsolete since this was answered, try using the Microsoft.AspNet.Web.Optimization instead. See this question for more info: stackoverflow.com/questions/9475893/… Feb 2, 2013 at 18:03
  • 3
    Clarification: The System.Web.Optimization namespace is NOT deprecated, the System.Web.Optimization.dll is.
    – Despertar
    Jun 1, 2013 at 4:23
  • Thanks! It is working for me when I got the trouble with the Layout of the different AREA.
    – NoWar
    Sep 17, 2013 at 14:30
47

There was one small step missing from the above, which I found on another post. After adding

<add namespace="System.Web.Optimization" />

to your ~/Views/web.config namespaces, close and re-open Visual Studio. This is what I had to do to get this working.

2
  • 2
    Same here, restart of VS worked but before that it still showed the error... ?
    – Andreas
    Feb 17, 2016 at 8:47
  • Closing VS was not necessary in my case but I had to close and re-open the .cshtml view I was using
    – joalcego
    Aug 30, 2017 at 20:30
31

I am using areas, and have just come up against this issue, I just copied the namespaces from the root web.config to the areas web. config and it now works!!

    <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" />
0
15

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 <add namespace="System.Web.Optimization"/> to both web.config files
  • Run Install-Package -IncludePrerelease Microsoft.AspNet.Web.Optimization
  • Update Bundling code
  • Update Layout file
  • The last step is to update 10 other libraries. I didn't and it worked fine. So looks like you can procrastinate this one (unless I already updated 1 or more of them). :)
3
  • Thanks for answering this Dan! (And pointing out my blog). His question implied he started with RC, but I think you're right. He just added a controller with RC to a Beta project. Jun 18, 2012 at 21:27
  • It has been a couple of weeks and you both certainly have me second guessing myself. I certainly put VS 2012 onto my tablet which had never had anything but Metro Apps and saw it fail. I will try again in a VM. Jun 28, 2012 at 23:27
  • This worked for me. Read the linked article for exact instructions on what you need to do.
    – Justin
    Jul 4, 2012 at 21:01
11

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:

Install-Package -IncludePrerelease Microsoft.AspNet.Web.Optimization

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.

5

Create a new MVC 4 RC internet application and run it. Navigate to Login which uses the same code

 @section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

What allows Login.cshtml to work is the the Views\Web.config file (not the app root version) contains

<namespaces>

    <add namespace="System.Web.Optimization"/>

  </namespaces>

Why is your Create view not working and Login is?

1
  • I wasn't using Login - that may well have been broken at the time. Jul 16, 2012 at 22:36
5

Import System.Web.Optimization on top of your razor view as follows:

@using System.Web.Optimization
3

I ran into this problem, however while running the command:

Install-Package -IncludePrerelease Microsoft.AspNet.Web.Optimization 

I received the cryptic message (gotta love a great pun before the first cup of coffee):

Install-Package : The specified cryptographic algorithm is not supported on this platform.

I am running this on Windows XP SP3 (not by choice) and what I found was that I had to follow the instructions posted by the user artsnob on the ASP.NET Forum

  • Please uninstall the Nuget and try re-installing it. If you are unable to do this, login as an Administrator.
  • Go to Tools=> Extension Manager => Select "Nuget Package Manager" => UnInstall
  • Install it again, by searching "Nuget" => Install.
  • If it did not work, please try installing, 1.7.x version as I mentioned in the previous post (It doesn't mean, you have to use the previous version, if it works fine, we can report this bug, and get the patches for the latest version).

Once I ran this I could then run the command line to update the Web.Optimization.

Hope this saves someone some digging.

2

Just write

@section Scripts{
    <script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/jqueryval")"></script>
}
3
  • @archil Thanks for the replacement - that will be a good temporary solution. But the main question was "Why does Scripts not exist?" Jun 8, 2012 at 16:43
  • @Jamie should it exist? I mean, as far as I remember, there is no Scipts helper out there. Maybe you are trying to use some third-party helper?
    – archil
    Jun 8, 2012 at 16:50
  • 1
    I didn't add that code myself - Visual Studio 2012 RC added it when it created the View for my Create Action. I think somehow my MVC 4 is not the right one. It must work somewhere because Scott Hanselman makes mention of the failing syntax as being a new addition to MVC 4 RC: hanselman.com/blog/… Jun 8, 2012 at 17:42
1

I upgraded from Beta to RC and faced 'Scripts' does not exist issue. Surfed all over the web and the final solution is what N40JPJ said plus another workaroudn:

Copy the following in View\Web.config :

  <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"/>
    <add namespace="System.Web.Helpers"/>
  </namespaces>

and the following inside View\Shared_Layout.cshtml

@if (IsSectionDefined("scripts")) 
{
       @RenderSection("scripts", required: false)
}

Hope it helps.

1
  • 1
    There is no need to use if (IsSectionDefined()) unless you are providing an else clause. The fact that you are saying required: false already allows that section to not be defined, and in that case the section will be omitted. So this is unnecessary extra code.
    – ErikE
    Jan 14, 2016 at 18:47
0

Apparently you have created an 'Empty' project type without 'Scripts' folder. My advice -create a 'Basic' project type with full 'Scripts' folder.

With respect to all developers.

0

just remove/ hide the code from create & Edit razor view of your controller.

 @section Scripts {  

   @Scripts.Render("~/bundles/jqueryval")  
 }  
0
  1. : remove view page.
  2. : add namespace webconfig (in view directory)
  3. : create view an try!

good luck...

0

One more for the pot - spent ages trying to work out the same problem - even though it was defined in the web.config for root and the root of Views. Turns out I'd mistakenly added it to the <system.web><pages><namespaces>, and not <system.web**.webPages.razor**><pages><namespaces> element.

Really easy to miss that!

0

When I enter on a page that haves this code:

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

This error occurs: Error. An error occurred while processing your request.

And this exception are recorded on my logs:

System.Web.HttpException (0x80004005): The controller for path '/bundles/jqueryval' was not found or does not implement IController.
   em System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
...

I have tried all tips on this page and none of them solved for me. So I have looked on my Packages folder and noticed that I have two versions for System.Web.Optmization.dll:

  • Microsoft.AspNet.Web.Optimization.1.1.0 (v1.1.30515.0 - 68,7KB)
  • Microsoft.Web.Optimization.1.0.0-beta (v1.0.0.0 - 304KB)

My project was referencing to the older beta version. I only changed the reference to the newer version (69KB) and eveything worked fine.

I think it might help someone.

0

That has an obvious solution. I had the same problem later. Not related to Assembly References or ... .It'll occur In hierarchy calling of MVC Partial views, when you have complicated page structures. So calling/rendering each part separately on each page (maybe a master page or partial) will cause to not see required parts of page like the bellow code :

@RenderSection("Scripts", required: false)

That simply forces page to find and render related section and in case of failure shows you an error message like you.

So I suggest you to trace your pages (like program trace) from master to all of its partials to Detect Dependencies. Maybe it be a terrible work, but no other choices available here.

Not that according to my experience, some conditional situations in programming causes not to show you the right error causes the problem.

0

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.

0

For me this solved the problem, in NuGet package manager console write following:

update-package microsoft.aspnet.mvc -reinstall
0

When i started using MVC4 recently i faced the above issue while creating a project with the empty templates. Steps to fix the issue.

  1. Goto TOOLS --> Library Package Manager --> Packager Manager Console Paste the below command and press enter Install-Package Microsoft.AspNet.Web.Optimization Note: wait for successful installation.
  2. Goto Web.Config file in root level and add below namespace in pages namespace section. add <namespace="System.Web.Optimization" />
  3. Goto Web.Config in Views folder and follow the step 2.
  4. Build the solution and run.

The Package mentioned in step 1 will add few system libraries into the solution references like System.Web.Optimization is not a default reference for empty templates in MVC4.

I hope this helps. Thank you

0

I had a very similar error when upgrading a project from MVC3 to MVC4.

Compiler Error Message: CS0103: The name [blah] does not exist in the current context

In my case, I had outdated version numbers in several of my Web.Configs.

  1. I needed to update the System.Web.Mvc version number from "3.0.0.0" to "4.0.0.0" in every Web.Config in my project.
  2. I needed to update all of my System.Web.WebPages, System.Web.Helpers, and System.Web.Razor version numbers from "1.0.0.0" to "2.0.0.0" in every Web.Config in my project.

Ex:

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

...

<compilation debug="true" targetFramework="4.0">
  <assemblies>
    <add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  </assemblies>
</compilation>

Be sure to review the Web.Configs in each of your Views directories.

You can read more about Upgrading an ASP.NET MVC 3 Project to ASP.NET MVC 4.

0

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.

0

I solve this problem in MvcMusicStore by add this part of code in _Layout.cshtml

@if (IsSectionDefined("scripts")) 
{
       @RenderSection("scripts", required: false)
}

and remove this code from Edit.cshtml

 @section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
-1

Try this:

@section Scripts 
{
    Scripts.Render("~/bundles/jqueryval")  // <- without ampersand at the begin
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.