View Single Post
  #7 (permalink)  
Old August 2nd, 2016, 07:04 PM
Matt_Lee Matt_Lee is offline
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Aug 2016
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default How to bundle scripts

Hello Imar

After following your clear instructions in bundling and minifying CSS files, it has worked out very smoothly for me.

However, I am not having the same experience in trying to enable bundling and minifying scripts.

I used the exact same approach for CSS, but tried to customize it for Scripts as follows:

In the Global.asax file, I added:
BundleTable.Bundles.Add(New ScriptBundle("~/ScriptSheets").IncludeDirectory("~/Scripts", "*.js"))

and in the MasterPage, I added:
<webopt:BundleReference runat="server" Path="~/ScriptSheets" />

After adding this, I see these kind of errors in my web console:
The stylesheet http://localhost:64961/Scripts/jquery-2.1.4.js was not loaded because its MIME type, "application/javascript", is not "text/css".

In researching online, I found there appears to be a different way to reference the newly created bundled files that includes referring to the type of file. Here is what I tried using (placed just under </asp:ScriptManager>):

<asp:PlaceHolder runat="server">
<%: Web.Optimization.Scripts.Render("~/ScriptSheets") %>
<%: Web.Optimization.Styles.Render("~/StyleSheets") %>

I thought this would help since it is specifying the type of file (e.g. ".Scripts"). But by using this instead of webopt, I no longer see the bundled files when I "View Page Source".

I also have some so-called "Advanced" ASP books (not bad, but I've found not as good as your "Beginning" book), but none show what syntax to use to properly implement bundling/minifying scripts.

Can you tell me what am I missing? Thank you for your time in advance.
Reply With Quote