459
votes

I'm trying to use the new bundling feature in a project I recently converted from MVC 3 to MVC 4 beta. It requires a line of code in global.asax, BundleTable.Bundles.RegisterTemplateBundles();, which requires using System.Web.Optimization; at the top.

When I do this, I get the red squiggly lines that say, "Are you missing an assembly reference?" When I try and add reference, and click on the .NET tab in the dialog, sort from A-Z, I do not see System.Web.Optimization.

How do I add this reference to my project?

7
I already had Optimization but it was causing an issue with the ScriptBundle which I could only resolve after uninstalling Opti... then reinstalling it.Myzifer
@Myzifer You should submit your comment as an answer. It was the only thing that worked for me to get the System.Web.Optimization node back into my References.Snekse
@myzifer Your answer is the correct one for this odd behavior. The only thing that would resolve this issue for me is running the nuget cmd line : UnInstall-Package Microsoft.AspNet.Web.Optimization and then right after that running the install : Install-Package Microsoft.AspNet.Web.Optimization. Tried to get this to work for weeks on my home computer with VStudio 2013 & never could get it. Thanks very much.raddevus
@Myzifer - worked for me also - "Checked out" a project from VSOnline and had this error - uninstall then reinstall - 20second job - thanks - this is one of those stupid problems you can easily lose a day to!Percy

7 Answers

734
votes

Update
Version 1.1.x is available, read the release notes: https://www.nuget.org/packages/Microsoft.AspNet.Web.Optimization


The Microsoft.Web.Optimization package is now obsolete. With ASP.NET (MVC) 4 and higher you should install the Microsoft ASP.NET Web Optimization Framework:

  • Install the package from nuget:

    Install-Package Microsoft.AspNet.Web.Optimization
    
  • Create and configure bundle(s) in App_Start\BundleConfig.cs:

    public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles) {
            bundles.Add(new ScriptBundle("~/Scripts/jquery").Include(
                "~/Scripts/Lib/jquery/jquery-{version}.js",
                "~/Scripts/Lib/jquery/jquery.*",
                "~/Scripts/Lib/jquery/jquery-ui-{version}.js")
            );
    
            bundles.Add(new ScriptBundle("~/Scripts/knockout").Include(
                 "~/Scripts/Lib/knockout/knockout-{version}.js",
                 "~/Scripts/Lib/knockout/knockout-deferred-updates.js")
            );
        }
    }
    
  • Call the RegisterBundles() function from Application_Start() in your global.asax.cs:

    using System.Web.Optimization;
    
    protected void Application_Start() {
         ...
         BundleConfig.RegisterBundles(BundleTable.Bundles);
         ...
    }
    
  • In your view.cshtml include the Optimization namespace and render the bundle(s):

    @using System.Web.Optimization
    
    @Scripts.Render("~/Scripts/jquery")
    @Scripts.Render("~/Scripts/knockout")
    

See http://www.asp.net/mvc/overview/performance/bundling-and-minification for more information

83
votes

With the final released version of ASP.Net MVC 4 the approach is as follows:

  • Install Microsoft.AspNet.Web.Optimization via nuget (as it is not installed by the framework)

    install-package Microsoft.AspNet.Web.Optimization
    
  • Create the bundle in Global.asax Application_Start:

    var scripts = new ScriptBundle("~/MyBundle");
    scripts.IncludeDirectory("~/Scripts/MyDirectory", "*.js");
    BundleTable.Bundles.Add(scripts);
    
  • Add the "System.Web.Optimization" namespace to the "Views" web.config:

     <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>
            <add namespace="System.Web.Optimization" />
        </namespaces>
    </pages>
    
  • In your view.cshtml add an include to the bundle created in the last step:

    @Scripts.Render("~/MyBundle")
    

In debug mode, all script files in your directory will render individually; in release mode they will be bundled and minified.

17
votes

Update (reinstall) the package from nuget, you can use the command:

update-Package Microsoft.AspNet.Web.Optimization -reinstall

14
votes

In my case it was a tfs issue, since tfs exclude binaries, so the Nugget PM find the nugget installed and don't update the library If you have similar issue :

  • Go to source control
  • Navigate to the ..\packages\Microsoft.Web.Optimization
  • Add lib folder (uncheck the exclude binary extensions)
  • Update your solution and add the dll reference from the path

NB : the package folder is in the same level of yousolution.sln file

11
votes

Using nuget to uninstall System.Web.Optimization in the package manager console like this:

Uninstall-Package Microsoft.AspNet.Web.Optimization

Then reinstalling using:

Install-Package Microsoft.AspNet.Web.Optimization

May solve this problem for you.

6
votes

Install it from NUGet through Visual Studio Open Visual Studio 2010 , select Tools-> Library Package Manager-> Package Manager Console

This will open the conslve, paste

Install-Package Microsoft.AspNet.Web.Optimization 

and enter. and you are done

2
votes

set in Global.asax application_start (in RELEASE mode etc.) :

BundleTable.EnableOptimizations = **true**;

to enable minification and change to false in DEBUG mode to render all script and style files individually.