83

In my mvc application, In the _Layout.cshtml has code below...

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

</head>

But the problem is it says The name 'Scripts' does not exists in the current context.

I have already added the assembly to the reference and to the Bundle config as using System.Web.Optimization;

This happens for @styles also.. What should I do?

tarzanbappa
  • 4,930
  • 22
  • 75
  • 117

7 Answers7

157

Make sure your ~/Views/Web.Config adds the System.Web.Optimization namespace:

<system.web.webPages.razor>
    <pages>
      <namespaces>
        <add namespace="System.Web.Optimization"/>
      </namespaces>
    </pages>
</system.web.webPages.razor>
TylerH
  • 20,799
  • 66
  • 75
  • 101
haim770
  • 48,394
  • 7
  • 105
  • 133
  • your solution worked, error has gone but still not able to get intelligence in .cshtml pages, any idea why? – Mox Shah Feb 17 '15 at 09:33
  • You don't have Intellisense at all? Or just for `Styles` and `Scripts`? – haim770 Feb 17 '15 at 09:35
  • No Intellisense at all in views (.cshtml). – Mox Shah Feb 17 '15 at 09:37
  • 2
    This has nothing to do with `System.Web.Optimization`. But see http://stackoverflow.com/questions/22832435/mvc-razor-view-intellisense-broken-in-vs-2013 – haim770 Feb 17 '15 at 09:44
  • 1
    Nope, earlier it was throwing error `The name 'Scripts' does not exists in the current context` which has gone after adding `System.Web.Optimization` in view's web.config, but yeah intellisense is still same, okay thanks let me check that question. – Mox Shah Feb 17 '15 at 09:46
  • 6
    For intelligence problem after adding the web.optimization in config file. Close all the open tab in visual studio. Clean the project and build the project. Open the .cshtml file and check whether intelligence work or not if not then again rebuild it. Now it should work. – Dipitak May 08 '17 at 22:19
  • @Dipitak EXCELLENT suggestion. Closed my Layout.cshtml page, did a rebuild on my solution, re-opened the page. It took it about 15 seconds or so still, after opening the page up, to get it, but it did finally clear those red squigglies & recognize `Scripts` and `Styles`. Wow... – vapcguy Aug 28 '18 at 22:40
  • Note: I also needed to include the `@using System.Web.Optimization` statement in the _Layout.cshtml file. I have a project where this is not necessary and it works fine, but for some reason in this project it needed it. – vapcguy Aug 28 '18 at 22:56
  • 1
    This worked for me, but only once I installed Microsoft.Aspnet.Web.Optimization from NuGet - possibly because I upgraded my project from 4.6.0 to 4.6.2, but worth checking for anyone else having issues – Jon Story Sep 11 '19 at 15:25
39

The following in .chtml solves the problem

@using System.Web.Optimization
@Scripts.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
zapoo
  • 1,548
  • 3
  • 17
  • 32
  • 2
    It worked!! but why it doesn't work when adding namespace in **Views/web.config** – Asad Shakeel May 24 '18 at 08:06
  • @AsadShakeel Had the same issue & got mine working the same way. I found if you then removed `@using System.Web.Optimization` again & closed the _Layouts.cshtml file & rebuilt the solution, then re-opened the file, the intellisense was broken again. Putting the line back, without a rebuild, immediately restored it. This was in a project that didn't originally have a BundleConfig class (& wasn't registered in Global.ascx.cs) and System.Web.Optimization wasn't originally in my web.config. Added all that manually. In projects that had the BundleConfig.cs from the beginning, never had this issue. – vapcguy Aug 28 '18 at 22:51
  • @hechchatt : here you are the reposne – merrais May 28 '19 at 11:24
18

Please Follow My Step will Clear this

First

  • Install Microsoft.AspNet.Web.Optimisation
  • Check that the App_Start Folder contains a BundleConfig.cs File.
  • Add <namespaces> <add namespace="System.Web.Optimization"/> </namespaces> in Views/web.config File
  • Add BundleConfig.RegisterBundles(BundleTable.Bundles); in Global.asax.cs
  • Rebuild and Run it
Abdulla Sirajudeen
  • 1,269
  • 1
  • 16
  • 29
3

There is still intellisense problem, you can add this code to Application_Start() method that inside global.asax file.

BundleConfig.RegisterBundles(BundleTable.Bundles);
slfan
  • 8,950
  • 115
  • 65
  • 78
erakm
  • 106
  • 2
  • This could be a part of the problem, but more accurate to include ensuring the `System.Web.Optimization` namespace is registered (Referenced Assemblies, web.config, `using` statement...). – vapcguy Aug 28 '18 at 22:53
1

In VisualStudio 2019,I was able to solve this error. Microsoft.Web.Optimization is now included in the 'Microsoft.AspNet.Web.Optimization' package. Using NuGet Packing Manager download the package. In .cshtml file add "@using System.Web.Optimization;

0

The easiest way to fix this issue is by downloading (Microsoft.AspNet.Web.Optimization) from Nuget Package Manager.

Steps:

  1. Right-click on the Solution.
  2. Choose "Manage Nuget packages for solutions"
  3. Search for (Microsoft.AspNet.Web.Optimization) and download--> apply it on the project.
  4. Close all the open pages in Visual Studio, and re-open them,
Dharman
  • 30,962
  • 25
  • 85
  • 135
reaz
  • 735
  • 1
  • 10
  • 20
0

you probably need to add the nuget package

Microsoft.AspNet.Web.Optimization

to your project.