Search code examples
c#asp.netwebformsbundling-and-minificationsystem.web.optimization

asp.net Webforms Bundling & minification - Bundled script throws 404


I have been trying to implement bundling and minification for my existing asp.net webforms project and unfortunately it is ending up with 404 responses.

Let me first share the steps I have followed.

  1. Installed System.Web.Optimization through nuget.

  2. Added BundleConfig class and registered it on Glabal.asax Application_Start event

My code to bundle the JS files.

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/modelJs").Include( 
    "~/src/model.js", 
    "~/src/calender.js", 
    "~/src/common.js", 
    "~/src/insurance.js")); 

...

  1. Added following markup to aspx page
 <asp:PlaceHolder runat="server"> 
    <%: System.Web.Optimization.Scripts.Render("~/bundles/modelJs") %> 
    </asp:PlaceHolder>
  1. set BundleTable.EnableOptimizations = true; in bundleConfig and the url formed is

http://localhost:9011/bundles/modelJs/?v=xNmmFhbzC1isUARLQne-XoBRkWBWApbnRQX8AGvNxQY1

I have also checked the sample project which gets added in VS2013 webforms application and surprisingly, it seems to be working fine.

I have seen many questions with similar problems on SO but was not able to solve it.


Solution

  • Well, the solution turns out to be very simple. Just added '/' after bundling folder which is modeljs in this case.

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

    Unfortunately, I didn't find this in any available source codes so I ended up wasting a lot of time. :-/