asp.net 4.5脚本捆绑也没有bundeling也没有缩小

时间:2013-04-30 08:57:53

标签: asp.net scriptmanager bundling-and-minification

我将项目从asp.net 3.5升级到4.5,以便使用脚本捆绑和javascript的缩小。现在我把它全部运行了,脚本都出现在我定义的包中,但是它们没有在一个脚本中“捆绑”在一起而且它们没有被缩小。

继承我所拥有的......

default.aspx包含:

<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>

Global.asax中的Application_Start:

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"));

        }
    }
}

我甚至在web.config中关闭了调试,即使我尝试在scriptmanager中强制释放模式以及脚本引用。

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

输出仍然是几个引用:

<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>

我错过了什么? 如何让bundle中的所有脚本只输出一个引用并缩小代码?

谢谢,J

2 个答案:

答案 0 :(得分:1)

您的<asp:ScriptReference />根本没有输出捆绑包。我使用MVC从剃刀模板中引用捆绑包,如下所示:

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

导致这样的输出(注意src属性包括原始包名称):

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

如果您不使用剃须刀,可以尝试直接引用System.Web.Optimization.Scripts课程来调用Render()方法,而不是使用<asp:ScriptReference />

关于缩小,您需要为您的捆绑添加缩小变换,如下所示:

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);

答案 1 :(得分:1)

使用Scripts.Render代替ScriptManager

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

或代码背后:

System.Web.Optimization.Scripts.Render("~/bundles/MyBundle")