The name 'Scripts' does not exists in the current context in MVC

C#asp.net MvcRazor

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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>

Solution 2 - C#

The following in .chtml solves the problem

@using System.Web.Optimization
@Scripts.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")

Solution 3 - C#

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

Solution 4 - C#

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

BundleConfig.RegisterBundles(BundleTable.Bundles);

Solution 5 - C#

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;

Solution 6 - C#

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,

Solution 7 - C#

you probably need to add the nuget package

Microsoft.AspNet.Web.Optimization

to your project.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontarzanbappaView Question on Stackoverflow
Solution 1 - C#haim770View Answer on Stackoverflow
Solution 2 - C#zapooView Answer on Stackoverflow
Solution 3 - C#Abdulla SirajudeenView Answer on Stackoverflow
Solution 4 - C#erakmView Answer on Stackoverflow
Solution 5 - C#Vijay KumarView Answer on Stackoverflow
Solution 6 - C#reazView Answer on Stackoverflow
Solution 7 - C#Muhammad HamadaView Answer on Stackoverflow