Adding the new ASP.NET Web Optimization framework to MVC4 projects after manually upgrading them

After manually upgrading an ASP.NET MVC project to MVC4 using these instructions, how do you then set up the new CSS and JavaScript asset bundling and minimization features of the ASP.NET Web Optimization Framework in MVC4? The default templates have this all set up, but how do you do it by hand?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

  • Right click References then Manage NuGet Packages and add “Microsoft.AspNet.Web.Optimization” (or type Install-Package Microsoft.AspNet.Web.Optimization into the NuGet console).
  • In your Web.config file, add the following to <system.webServer>, allowing the minified bundles to be served with extensionless URLs.
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFrameworkv4.0.30319aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFramework64v4.0.30319aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  • In your App_Start folder, add a new class called BundleConfig.cs. It should look something like this:
using System.Web;
using System.Web.Optimization;

namespace MvcApplication1
    public class BundleConfig
        // For more information on Bundling, visit
        public static void RegisterBundles(BundleCollection bundles)
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(

            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(

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

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

            bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
  • Edit the above to add the script and stylesheet bundles you require then add the following lines to the using section and Application_Start in Global.asax.cs:
//using section
using System.Web.Optimization;

  • Replace your CSS and JavaScript and tags in _Layout.cshtml with calls to @Styles.Render("~/Content/css") and @Scripts.Render("~/bundles/jquery"), replacing the parameters with the names of the bundles you added to BundleConfig.cs. Make sure not to name any of the bundles the same as folders in your project.

You should now be all set – read up on how to use the full featureset here:

Method 2

Yes Follow the below steps to bundle and minify JS and CSS:

  • First open package manager console and run the command, select your web app as the project.

Install-Package Microsoft.AspNet.Web.Optimization

  • Go to global.asax right click and view code
  • Paste the below code:
    public static void MinifyJavaScriptAndCSS()
       var scripts1 = new ScriptBundle("~/bundles/customJSBundle");
        //Bundle Css
        var css1 = new StyleBundle("~/bundles/customCSSBundle");
  • Call this in Application_Start()
     protected void Application_Start()
  • Go to _Layout.cshtml in Views/Shared
  • Add the line in head

    @ViewBag.Title – My ASP.NET Application

  • Add this before closing of the body tag
     //your code
  • In web.config if you set compilation debug=true, files will not be bundled.
    If you set it as false, then the files will be bundled.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x