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"));
        }
    }
}
2
  • 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. Commented Apr 23, 2013 at 21:20
  • I've added more information...I'm not sure if there is anything else I can provide.
    – MacSalty
    Commented Apr 23, 2013 at 21:39

12 Answers 12

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

1
  • 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
    Commented Jan 31, 2014 at 17:28
59

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

0
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.

1
  • 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
    Commented Jul 14, 2016 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.

2
  • 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
    Commented Apr 23, 2013 at 21:59
  • Sorry, my bad, mvc projects don't have "Property pages" option.
    – sdagkas
    Commented Apr 23, 2013 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.
1
  • 1
    Update-Package -reinstall Microsoft.AspNet.Web.Optimization - it's a two-fer (not sure if nay differences, but it worked for me)
    – WernerCD
    Commented Dec 3, 2015 at 4:51
6

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

3

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

1

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

1

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

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

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.

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.