2

I'm trying to bundle my css/js-files in my webforms project but it's not working.

This is what I've done:

  • Included System.Web.Optimization via NuGet

  • Added the code below to global.asax.cs in Application_Start

    BundleTable.Bundles.Add(new ScriptBundle("~/bundle/js").Include("~/js/mainJS.js"));
    BundleTable.Bundles.Add(new ScriptBundle("~/bundle/css").Include("~/css/mainStyle.min.css"));
    
  • Added this to my masterpage

    <%: Scripts.Render("~/js/mainJS.js") %>
    <%: Styles.Render("~/css/mainStyle.min.css") %>
    

It renders the references to the css/js-files so the page is working but it's not generating a unique file name and it's not refering to the bundle folder like I expected.

I'm using VS2013 and Framework 4.0.

6
  • Are you compiling to release mode? Commented Nov 3, 2013 at 20:45
  • Yes, I'm compiling in release mode Commented Nov 3, 2013 at 21:13
  • Are you sure you have <compilation debug="false" /> in your web.config (this is independent from building in release mode)? Commented Nov 4, 2013 at 10:48
  • this is from my web.config file: <compilation targetFramework="4.0" debug="false"/> Commented Nov 4, 2013 at 18:47
  • I tried to do it from the code file and ad it to a asp:PlaceHolder like this this.PlhHeader.Controls.Add(new LiteralControl(Scripts.Render("~/bundles/js").ToHtmlString())); but it still just generates <script src="/js/mainJS.js"></script> Commented Nov 4, 2013 at 19:48

2 Answers 2

1

You have to Render() using the bundle name ("~/bundle/js") not the javascript file name ("~/js/mainJS.js").

<%: Scripts.Render("~/bundle/js") %>
<%: Styles.Render("~/bundle/css") %>
Sign up to request clarification or add additional context in comments.

Comments

-1

Try doing BundleTable.EnableOptimizations = true; inside your Application_Start event of Global.asax. Read this blog post for more information.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.