15

I would like to have define a bundle like this:

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

If the bundle name is just "~/style" this works, but with the file extension it always returns a 404. I suspect the server searches for CSS and JS files on the drive and ignores the bundling system, but I can't find anyone else who is trying to include file extensions in bundle names. Is this possible to do without something like a URL rewrite?

Hao Kung
  • 28,040
  • 6
  • 84
  • 93
sbking
  • 7,630
  • 24
  • 33

1 Answers1

24

You could add the following to your <system.webServer> section in web.config:

<modules runAllManagedModulesForAllRequests="true" />

This will ensure that requests for static resources such as .js and .css will pass through the managed modules and be intercepted by ASP.NET MVC.

As an alternative to enabling runAllManagedModulesForAllRequests for all requests you could configure them only for the urls you need to use. So inside the <handlers> add the following:

<handlers>
    <!-- ... -->
    <add name="scriptBundle" verb="*" path="script.js" type="System.Web.Optimization.BundleHandler, System.Web.Optimization" preCondition="managedHandler" />
    <add name="cssBundle" verb="*" path="style.css" type="System.Web.Optimization.BundleHandler, System.Web.Optimization" preCondition="managedHandler" />
</handlers>
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 3
    Hmm, is there a less drastic solution that would only affect .js and .css files? – sbking Oct 17 '12 at 09:57
  • 1
    +1 but having `path="*.bundle.js"` will prevent the handler from running for real files. – Fabio Milheiro Jul 28 '14 at 08:14
  • I am wondering if the second solution (adding the individual handlers) has some drawbacks. It certainly appears to work in some cases but I did also ocassionally see an error like `Unhandled application error: System.MissingMethodException: Constructor on type 'System.Web.Optimization.BundleHandler'` in our logs. Certainly, BundleHandler does not have a parameter-less constructor. How is this supposed to work (i.e. which behavior are qe relying on?) – Johannes Rudolph Jun 15 '15 at 07:14
  • re. my previous comment, I have confirmed the exception occurs for request that point to a proper bundle path but with a wrong bundle version parameter (e.g. `/x/bundle.js?v=12345`). I'm sure this can be fixed (and a proper 404 returned) with some other trickery.... – Johannes Rudolph Jun 15 '15 at 07:27