461

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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Ian Davis
  • 19,091
  • 30
  • 85
  • 133
  • 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 Jun 20 '13 at 08:46
  • 2
    @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 Apr 29 '14 at 16:39
  • 2
    @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 Jul 19 '15 at 22:11
  • @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 Nov 26 '15 at 11:50

7 Answers7

738

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

TylerH
  • 20,799
  • 66
  • 75
  • 101
mhu
  • 17,720
  • 10
  • 62
  • 93
  • 2
    Do you need MVC4 to use this? – FloatLeft Jun 11 '12 at 15:03
  • @FloatLeft: No, its part of ASP.NET – mhu Jun 12 '12 at 09:08
  • This answer is out of date. Refer to the below answer from Noel Abrahams – Pat James Sep 27 '12 at 18:01
  • 5
    It's not out of date. This is how I use it with the final releases of Visual Studio 2012 and ASP.NET (MVC) 4. – mhu Sep 28 '12 at 06:55
  • 29
    Your answer includes "using System.Web.Optimization;", which is exactly the thing that the questioner is asking about. – davidpricedev Nov 15 '12 at 22:37
  • 10
    @dave: Is that good or bad? To solve the "missing assembly reference" Microsoft.AspNet.Web.Optimization should be installed as mentioned in the answer. – mhu Dec 19 '12 at 09:29
  • 6
    in the default MVC template i could use Scripts class without adding System.Web.Optimization namespace on the top of the page. But in my progect i get error "the name Scripts does not exist". How can i resolve that? Adding this namespace in web.config didn't help. Edit: reloading solution helped, sorry :) – Wachburn Jul 11 '13 at 12:14
  • i do follow your step, and i still got error, took alot of time to find out that, when i rebuild, it still get OLD Optimization and alert "missing", for anyone need that, DELETE ALL FIND DLL in BIN\DEBUG when u still building ERROR. – Hua Trung Jun 26 '18 at 19:47
  • Note that you need to add `using WebApplication1.App_Start;` at the top of the Global.asax.cs, too, where `WebApplication1` is the namespace of your project. – vapcguy Aug 28 '18 at 21:48
  • For those that might get intellisense errors on the `view.cshtml` part, you might need to save your page, close it, rebuild your solution, then open it again. There's also this question that deals with issues related to that: https://stackoverflow.com/questions/23534516/the-name-scripts-does-not-exists-in-the-current-context-in-mvc – vapcguy Aug 28 '18 at 22:59
  • And make sure the NuGet installation adds it to your web.config, like it has in another answer: ` ` – vapcguy Aug 30 '18 at 23:44
83

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.

Liam
  • 27,717
  • 28
  • 128
  • 190
  • Correct, this version includes it. The other version does not. – Luke Belbina Mar 26 '12 at 23:07
  • 1
    I can't understand why this doesn't work for me. I've tried both this way and to create the bundle in BundleConfig.cs but when I add @Styles.Render("~/CssBundle") in my view all I get is that gives me 404. Any idea why? – Misbit Aug 22 '14 at 13:56
  • @vsdev I know this is 4 years too late, but you might have to check your BundleConfig.cs to be sure you're adding it correctly - the line in this answer is only referencing `".js"` files. Inside `public static void RegisterBundles(BundleCollection bundles) { ... }` you should be able to do something like `bundles.Add(new StyleBundle("~/CssBundle"));` and it would grab all CSS files in your `CssBundle` folder. You don't want `ScriptBundle` like it has in the example for CSS files. – vapcguy Aug 28 '18 at 23:05
17

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

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

Mike Elofson
  • 2,017
  • 1
  • 10
  • 16
M. Salah
  • 671
  • 7
  • 10
14

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

amine
  • 320
  • 3
  • 7
12

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.

TylerD87
  • 1,588
  • 11
  • 20
Myzifer
  • 1,116
  • 1
  • 20
  • 56
  • 1
    Not sure how this answers the question? – TylerD87 Sep 04 '15 at 08:25
  • @TylerD87 Simple it solves a particular set of circumstances and I'm not the only one who found this to be the case, if you read the comments on the question you'll notice Snekse said on Apr 29 '14 at 16:39 that they to needed to follow the particular instructions I gave and that below someone else posted much the same much later and was upvoted. – Myzifer Sep 04 '15 at 08:34
  • Maybe edit your answer so it looks more like an answer? I didn't even realise you were answering the question or what you were suggesting. – TylerD87 Sep 04 '15 at 08:58
  • @TylerD87 The question was about Optimization and my answer was about Optimization, it solves the problem detailed in the question and has been acknowledged by at least 2 others, if you would offer details as to exactly how you've misunderstood my answer and how best I could alter/amend it to provide a wider audience a clearer answer then I'd be happy to make such changes but without constructive criticism I'm unable to determine how I might improve. – Myzifer Sep 04 '15 at 09:13
  • Hi, I have suggested an edit which makes it clearer what I was referring to. Having read through the comments at the top I see this is actually the suggestion that worked but the way your answer was presented was unclear about what you were actually suggesting. – TylerD87 Sep 04 '15 at 13:05
  • I see what you mean with my previous answer being less specific or direct as I think when I posted the answer it was more as a side addition, with the assumption that since the install code was covered in the question/other earlier answers that it was unnecessary to go to that level of detail. Which I suppose suffices for a comment but not an actual answer. – Myzifer Sep 04 '15 at 14:07
6

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

MayogaX
  • 469
  • 3
  • 18
Mian
  • 231
  • 5
  • 7
2

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.

zenichi
  • 124
  • 1
  • 3
  • 6
    It is easier to control this through the web.config. If `` is set (which is pretty standard when building a debug version), no optimization takes place. Normally this setting is automatically removed in a release build. `BundleTable.EnableOptimizations` should only be used to overrule the web.config. For more info: http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification (search for "Controlling Bundling and Minification") – mhu Jun 13 '13 at 20:11
  • This helped me so much, thank you. I had to disable it because working with dotless + RequestReduce was making the bundles garbled messes. – siva.k Mar 17 '14 at 18:53
  • This setting really depends on if you are using pre-minified files in your bundles, or if you are using non-minified ones - not so much if you are in Debug or Release, except for best practices for each of those modes - ex., you can have a project you are compiling in Debug mode, but where you have `BundleTable.EnableOptimizations = false;` because in your BundleConfig.cs, you are specifying all pre-minified files and you don't want it to do another minimization on them. – vapcguy Aug 30 '18 at 23:56
  • This would really only be the answer to someone's issue if they were using non-pre-minimized files (i.e. files WITHOUT .min) in their BundleConfig.cs. If they used .min files and set it to true, it would have problems. – vapcguy Aug 30 '18 at 23:59