Bundle Minification not working when publishing WebForms App

Turns out it was ASP.NET form authentication. As according to this, the name of the bundle should not be an existing directory. And well, forms authentication denies access to those directories that are not allowed int the web.config.

I did not know that the bundles create their own directory, so I basically added the location tag for those directories (even though they are not physically in the solution).

So basically...

For all of the previous bundles names, I added "~/bundles/" and then created the following location tag in the web.config:

<location path="bundles">
  <system.web>
    <authorization>
      <allow users="*"/>
    </authorization>
  </system.web>
</location>

As long as you don't try to bundle minified scripts, it should work. Sometimes if you have written javascript and you missed a semicolon, this could cause the scripts to fail when calling them, but not throw a 403 error.

Since you get a 403 error i'm guessing that this is not related to the bundling. Can you show the exact error? Is it a 403.2: Read access forbidden? Or is it when you try to publish to your site (403.3: Write access forbidden)? The best setup when developing is to have your local IIS setup properly so that you can bundle and publish a release to your local machine. If this works then all you need to do is to copy your files from your local machine to the public server. If something is wrong on the public server, then you know that it's a configuration error on that machine, and not a code problem.

My best guess is that you are not allowed to publish your application to your site (you get write access denied for some folders), but without further information it's really hard to say.

EDIT: After reading a little more about bundling I'm almost certain that you are bundling a script to an existing folder.

Every request in ASP.NET is manged through http handlers(for example, static handler, page handler, ashx handler, etc). There is a special HTTP Module called UrlRoutingModule which matches the routes in global.asax. If a route is match then it will chnage the current http handler using HttpContext.RemapHandler method otherwise normal ASP.NET flow will continue. Similarly System.Web.Optimization insert a BundleModule http module which try to match a binding. If a match is found found then it will select the BundleHandler as http handler using HttpContext.RemapHandler method. Internally System.Web.Optimization will leave a match if HostingEnvironment.VirtualPathProvider.FileExists(path) is true or HostingEnvironment.VirtualPathProvider.DirectoryExists(path) is true.

Read the whole thread

With this said, make all bundles start with "~/bundles/". This will ensure that the script will not point to a folder that exists or get caught by your routeconfiguration.

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