Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net 4.5 script bundles nor bundeling nor minifying

I upgraded a project from asp.net 3.5 to 4.5 in order to use script bundling and minification of javascript. Now I have it all running and the scripts all come out in the bundle that I defined but they are not "bundled" together in one script and they are not minified.

Heres what I have...

the default.aspx contains this:

<asp:ScriptManager
ID="scriptmanager"
LoadScriptsBeforeUI="false"
runat="server"
    ScriptMode="Release"
    EnableScriptLocalization="false"
>
    <Scripts>
        <%--Framework Scripts--%>
        <asp:ScriptReference Path="~/bundles/MyBundle" ScriptMode="Release"/>
        <%--Site Scripts--%>
    </Scripts>
</asp:ScriptManager>

Application_Start in Global.asax:

BundleTable.EnableOptimizations = true;
BundleConfig.RegisterBundles(BundleTable.Bundles);

BundleConfig.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;

namespace MyNameSpace
{
    public class BundleConfig
    {
        // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254726
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/MyBundle").Include(
                "~/Scripts/WebForms/WebForms.js",
                "~/Scripts/WebForms/MSAjax/MicrosoftAjax.js",
                "~/Scripts/WebForms/MSAjax/MicrosoftAjaxWebForms.js",
                "~/Scripts/WebForms/TreeView.js",
                "~/Scripts/WebForms/Focus.js",
                "~/Scripts/WebForms/MenuStandards.js"));

        }
    }
}

I even turned off debug in web.config even though I try to force release mode in the scriptmanager as well as the script reference.

<compilation debug="false" batch="true" targetFramework="4.5">

The output is still several references:

<script src="/MyProj/WebResource.axd?d=pynGkmcFUV13He1Qd6_TZA6EiyQ1YRW47qIzscWLzWU7jP_7DjoC2XbU7kCBkgYcJdoeAwqaVpUMnbWRsvhdMw2&amp;t=634896541540000000" type="text/javascript"></script>
<script src="/MyProj/ScriptResource.axd?d=zvkqIRNUspAvS1yKeFhMb4kS_IY-Q_9Yn_KOfmzKLnliETz8uip5T2BUr1JOPE4XV1bmnifY3Eg8vrX8bPLYT71P0Kf8DwEcoRw5fj2tqHdQSorRXVpasfsMXeJLHbT_alkHjf2wIrgxLzxYvocKIA2&amp;t=12e197aa" type="text/javascript"></script>
<script src="/MyProj/ScriptResource.axd?d=NJmAwtEo3Ipnlaxl6CMhvpbyEkpQU7AWZ3ZOrSRn7cdqTBUwP_3lu0l46EnEFBAkBOoC5I7IpMnx8u7VKe4fESCWGvycDq7dTXHsUSTg-j9u4S2Poz38UkmBa7Ta1cXyZ9DcFfKo7RpgjmNNoFjlZgbsHJEPN_AnazProCOQuws1&amp;t=6119e399" type="text/javascript"></script>
<script src="/MyProj/ScriptResource.axd?d=dwY9oWetJoJoVpgL6Zq8OPgCkw9mWNaQmhnwlbrOgCXqxXAsNin9JxaUjtv38gzHz78sozRMjuXYeM_GE0v4htSt6vWwOO4-gOCLfSt6rVOvxbpcCXCLN9jI7fWPkTL2Eq7a9kcN8S4MasueIxDV0rhf-htD32XuwA259deRSNs1&amp;t=6119e399" type="text/javascript"></script>
<script src="/MyProj/WebResource.axd?d=JoBkLzP19aTuxbWOhHobYmKZKUo0k_GoCFbuT9i-BqZJQhy_7Dl0oCPSUy3hiGltbFyiUTxWBZD-5YWblJpSZg2&amp;t=634896541540000000" type="text/javascript"></script>

What am I missing? How can I get all scripts in the bundle to output only one reference and minify the code within?

Thanks, J

like image 804
JGoodgive Avatar asked Nov 02 '22 22:11

JGoodgive


2 Answers

It doesn't appear that your <asp:ScriptReference /> is outputting the bundle at all. I reference the bundle from a razor template using MVC like this:

@Scripts.Render("~/bundles/lib")

which results in output like this (notice the src attribute includes the original bundle name):

<script src="/bundles/lib?v=gGKSj7TFmjDTZAjdEzBrqOOu9aGB6i4Tq0mHfaLUk_c1"></script>

If you're not using razor you might try directly referencing the System.Web.Optimization.Scripts class to call the Render() method and NOT using the <asp:ScriptReference />.

Regarding minification, you need to add a minification transform to your bundle like this:

var myBundle = new ScriptBundle("~/bundles/MyBundle").Include(
                "~/Scripts/WebForms/WebForms.js",
                "~/Scripts/WebForms/MSAjax/MicrosoftAjax.js",
                "~/Scripts/WebForms/MSAjax/MicrosoftAjaxWebForms.js",
                "~/Scripts/WebForms/TreeView.js",
                "~/Scripts/WebForms/Focus.js",
                "~/Scripts/WebForms/MenuStandards.js")
myBundle.Transforms.Add(new JsMinify());
bundles.Add(myBundle);
like image 130
jandersen Avatar answered Nov 11 '22 16:11

jandersen


Use Scripts.Render instead of ScriptManager

<%: Scripts.Render("~/bundles/MyBundle") %>

Or in code behind:

System.Web.Optimization.Scripts.Render("~/bundles/MyBundle")
like image 25
Cyrus Avatar answered Nov 11 '22 14:11

Cyrus