35

I'm working on an MVC 4 project started in Visual Studio 2010. Right now I'm working on a machine with Visual Studio 2012 as I don't have access to the machine I was originally working on. I tried all morning to find answers, but they don't seem to help my situation.

I followed How to add reference to System.Web.Optimization for MVC-3-converted-to-4 app and installed from nuget right into my solution. Even though I have all the required reference packages downloaded and installed on my machine, System.Web.Optimization continues to remain missing. Is there anything else I can do?

Edit: When I try to build the project I receive the following errors

  • The type or namespace name 'Optimization' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) - BundleConfig.cs
  • The type or namespace name 'Optimization' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) - Global.asax.cs
  • The type or namespace 'BundleCollection' could not be found (are you missing a using directive or an assembly reference?) - BundleConfig.cs

My BundleConfig.cs contains

using System.Web;
using System.Web.Optimization;

namespace BCCDC_AdminTool
{
    public class BundleConfig
    {
     // For more information on Bundling, visit http://go.microsoft.com/fwlink/?    LinkId=254725
    public static void RegisterBundles(BundleCollection bundles)
    {
           bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

           bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                    "~/Scripts/jquery-ui-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.unobtrusive*",
                    "~/Scripts/jquery.validate*"));

        // Use the development version of Modernizr to develop with and learn from. Then, when you're
        // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

            bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

            bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                    "~/Content/themes/base/jquery.ui.core.css",
                    "~/Content/themes/base/jquery.ui.resizable.css",
                    "~/Content/themes/base/jquery.ui.selectable.css",
                    "~/Content/themes/base/jquery.ui.accordion.css",
                    "~/Content/themes/base/jquery.ui.autocomplete.css",
                    "~/Content/themes/base/jquery.ui.button.css",
                    "~/Content/themes/base/jquery.ui.dialog.css",
                    "~/Content/themes/base/jquery.ui.slider.css",
                    "~/Content/themes/base/jquery.ui.tabs.css",
                    "~/Content/themes/base/jquery.ui.datepicker.css",
                    "~/Content/themes/base/jquery.ui.progressbar.css",
                    "~/Content/themes/base/jquery.ui.theme.css"));
        }
    }
}
Community
  • 1
  • 1
MacSalty
  • 1,212
  • 4
  • 14
  • 21
  • Are you getting some error message? If so please state more clearly if it is a compile-time or runtime error that you are getting with the exact error message. If not, please explain more clearly the precise problem you are currently encountering. – Darin Dimitrov Apr 23 '13 at 21:20
  • I've added more information...I'm not sure if there is anything else I can provide. – MacSalty Apr 23 '13 at 21:39

12 Answers12

79

I had the same problem with a VS2010 project I opened in VS2012. The Microsoft.AspNet.Web.Optimization package was missing so I just needed to download it using Nuget Package Manager.

You can run in the console: Install-Package Microsoft.AspNet.Web.Optimization

Jonathan Bick
  • 939
  • 2
  • 9
  • 14
  • Well, I think he tried that. Actually in his own words "I followed How to add reference to System.Web.Optimization for MVC-3-converted-to-4 app and installed from nuget right into my solution. Even though I have all the required reference packages downloaded and installed on my machine, System.Web.Optimization continues to remain missing. Is there anything else I can do?" – sdagkas Jan 31 '14 at 17:28
59

I know it's confusing, but you will need to Install-Package Microsoft.AspNet.Web.Optimization

Tristan Warner-Smith
  • 9,631
  • 6
  • 46
  • 75
rido
  • 1,202
  • 1
  • 9
  • 13
14

Try going to the web.config in the Views folder and removing the following line:

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

That fixed it for me.

Bryan Legend
  • 6,790
  • 1
  • 59
  • 60
  • This worked for me. My situation was that I added MVC 4 to an older Web Forms .NET 4.0 project. I added the Microsoft.AspNet.Web.Optimization nuget package but for some reason still got this error when the site was deployed to a live server. Removing this from the Views Web.config resolved the issue for me. – Finster Jul 14 '16 at 14:55
12

This is a long shot but here it goes anyway, right click on the project --> Property Pages and make sure System.Web.Optimization entry exists. If not, copy the System.Web.Optimization.dll to your bin folder or import it as a reference.

Edit: mvc projects don't have the "Property pages" menu option.

One other thing you can try is create a new mvc4 basic project, go to the bin folder of the solution and copy the System.Web.Optimization.dll to the bin folder of your other project that is giving you the error. If you can't find the dll then try updating the nuget packages.

sdagkas
  • 578
  • 1
  • 5
  • 20
  • This is where my lack of experience shines. I don't see anywhere that shows that kind of information on the Property Pages of my project. I also actually can't seem to find the System.Web.Optimization.dll anywhere on my PC... That does seem like a good solution to me though. – MacSalty Apr 23 '13 at 21:59
  • Sorry, my bad, mvc projects don't have "Property pages" option. – sdagkas Apr 23 '13 at 22:23
8

I battled this for an hour and none of the above suggestions worked. In the end only the following worked (for some obtuse reason) :

  1. ctrl-q -> type "package manager console"
  2. Uninstall-Package Microsoft.AspNet.Web.Optimization <problem project name>
  3. Install-Package Microsoft.AspNet.Web.Optimization <problem project name>

WARNING/HEADS-UP

  • the above will install the latest version of Microsoft.AspNet.Web.Optimization. If you want a particular version (say v1.1.1), use Install-Package Microsoft.AspNet.Web.Optimization <problem project name> -Version 1.1.1
  • don't forget the <problem project name> parameter or nuget will operate at the Solution-level, possibly trampling sibling projects.
  • Note that omitting the project name is an of itself really handy for doing Solution-level version updates or package installs.
sming
  • 801
  • 2
  • 12
  • 25
  • 1
    Update-Package -reinstall Microsoft.AspNet.Web.Optimization - it's a two-fer (not sure if nay differences, but it worked for me) – WernerCD Dec 03 '15 at 04:51
6

Go to tools >> Library Package Manager >> Manage Nuget Packages >> install Microsoft ASP.NET web optimisation framework

rakhesh
  • 61
  • 1
  • 1
3

Uninstalling, and reinstalling the Microsoft.AspNet.Web.Optimization package solved it for me.

Bravax
  • 10,453
  • 7
  • 40
  • 68
1

I fixed this issue by setting the reference to the assembly System.Web.Optimization.dll to copy local to true.

James
  • 12,636
  • 12
  • 67
  • 104
1

Reinstalling system.web.optimizaiton using Nuget package manager solved this issue for me..

Faisal
  • 29
  • 3
0

What worked for me in a specific scenario (I downloaded a MVC project from source control and received that error), was to open a new MVC project on the same solution. Afterwards I could delete the new project I've created and it still works (on VS2012).

BornToCode
  • 9,495
  • 9
  • 66
  • 83
0

Simple Solution. Open Visual Studio in administrator mode. It will resolve your error.

-2

I know there are many answers to this issue, but give this a try- open the project in visual studio as an Administrator.It did solve in my case.

WonderHeart
  • 678
  • 10
  • 28