Flame_Phoenix
Flame_Phoenix

Reputation: 17574

The type or namespace name 'BundleCollection' could not be found (are you missing a using directive or an assembly reference?)

So, I have a MVC 4 project in C# and I am using Visual Studio For Web 2012 Express.

I cannot compile the projecto due to the error: The type or namespace name 'BundleCollection' could not be found (are you missing a using directive or an assembly reference?)

Normally, this would mean that a library is missing. Thus after making a quick search on the internet I used NuGet to install Microsoft.AspNet.Web.Optimization, but that still did not work.

What makes this intriguing to me is that BundleCollections should be known to the application by deafult. I can only imagine that I have added a dependency that messed everything up, but I really can't know for sure.

How can I fix this problem? What am I missing here?

Code:

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

namespace Dockis
{

    public class BundleConfig
    {
        // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
        public static void RegisterBundles(BundleCollection bundles)
        {
            IItemTransform cssFixer = new CssRewriteUrlTransform();

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

           //...
        }
    }
}

EDIT

After checking my references folder I tried running the command Install-Package System.Web.Optimization, however I cannot install this package. I get the following error:

Install-Package : One or more errors occurred.
At line:1 char:16
+ Install-Package <<<<  System.Web.Optimization
    + CategoryInfo          : NotSpecified: (:) [Install-Package], AggregateException
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand

What is odd, is the fact that running Install-Package System.Web.Optimization.Less works, and fixes some depencie problems, but not all of them. Thus I believe I really need the first command to work.

What am I doing wrong?

Upvotes: 16

Views: 38155

Answers (6)

Ivan Gechev
Ivan Gechev

Reputation: 712

In my case this problem was fixed by closing Visual Studio, deleting the .vs and packages folders. Then I started Visual Studio again and ran Update-Package -reinstall in the Package Manager Console and finally installed Microsoft.AspNet.Web.Optimization.

Upvotes: 0

Oleksii Mykhailenko
Oleksii Mykhailenko

Reputation: 11

For broken projects:

  • Right-click on References -> Manage NuGet Packages
  • Find Microsoft.AspNet.Web.Optimization in the browse section
  • Downgrade it to the previous version
  • If everything is ok, then roll it back to the latest version

Should work for such kind of situations.

Upvotes: 1

codezealot
codezealot

Reputation: 39

This is just an addition to the solutions already identified for this issue. In my case, none of the above solutions worked. In the end, I had to manually locate the DLL in my .NET folders on my local drive and add the System.Web.Optimization.dll to my project manually.

Visual Studio 2017 indicated sucessful installation of the Microsoft.AspNet.Web.Optimization.1.1.3 package, however, did not allow the DLL to be added to the project. So even though the Nuget install was successful, the app would not compile. Manually adding the DLL to the project fixed it.

Upvotes: 1

Carlos Toledo
Carlos Toledo

Reputation: 2694

Install Microsoft.AspNet.Web.Optimization on the NuGet Package Manager. If it's already installed but not working try install a previous version and then the last version again. Worked for me, :)

Upvotes: 3

Flame_Phoenix
Flame_Phoenix

Reputation: 17574

There is no clear answer for this question. I managed to fix the project only by re-creating it, and then downloading all the DLLs in a different order, closing and re-opening the visual studio several times while doing it.

Aparently there was some conflict between some of the packages, but that was impossible to guess until I had re-created the project from scratch again.

Running Install-Package Microsoft.AspNet.Web.Optimization did solve the problem after the clean install however, so I recommend it.

I thank everyone for the help.

Upvotes: 35

voidzero
voidzero

Reputation: 594

PM> Install-Package Microsoft.AspNet.Web.Optimization

Upvotes: 8

Related Questions