【问题标题】:enable bundling only not minification in mvc4仅在 mvc4 中启用捆绑而不是缩小
【发布时间】:2013-07-15 07:13:25
【问题描述】:

我正在 MVC4 中实现捆绑和缩小,但是当我在 IIS 服务器上部署时它不起作用。我在我的 BundleConfig.cs 中使用了以下代码

public static void RegisterBundles(BundleCollection bundles)
{ 
    bundles.Add(new StyleBundle("~/Content/styles/siteCss").Include("~/Content/styles/reset.css")); 
    bundles.Add(new ScriptBundle("~/siteJsCommon").Include("~/Scripts/html5.js",
        "~/Scripts/jquery.js",
        "~/Scripts/jquery-migrate-1.1.1.js",
        "~/Scripts/jquery-ui-1.10.3.custom.js",
        "~/Scripts/carousel.js",
        "~/Scripts/template.js",
        "~/Scripts/jquery.validate.js",
        "~/Scripts/additional-methods.js",
        "~/Scripts/function.js"));

    BundleTable.EnableOptimizations = true;       
}

即使我检查了我的 web.config。看起来不错。

<compilation debug="false" targetFramework="4.5" />

谁能告诉我我在哪里做错了。 是否可以只启用捆绑包?

谢谢 阿舒

【问题讨论】:

  • 感谢您的回复。要做到这一点,最简单的方法是将 Script/StyleBundles 更改为默认没有设置 Transform 的普通 Bundle,这将关闭缩小但仍然捆绑。你能建议我怎么做 plain Bundles
  • 请建议我如何仅启用捆绑。

标签: asp.net-mvc-4 bundling-and-minification


【解决方案1】:

没有内置的配置/选项可让您在不缩小的情况下启用捆绑。

但是,Bundles(脚本或样式)使用IBundleTransform:Microsoft.Web.Optimisation 包括两个默认转换类型 JsMinify 和 CssMinify,分别由 ScriptBundle 和 StyleBundle 使用。但是,我们可以根据需要创建自己的自定义转换类型来处理引用,甚至最好不要使用IBundleTransform

所以,要在没有缩小的情况下启用捆绑,我们可以试试这个:

    //somewhere after all bundles are registered 
    foreach (var bundle in bundles)
    {
        bundle.Transforms.Clear();
    }

【讨论】:

  • 你是在调试还是发布模式下运行?
  • 我一一使用了这两种类型,但没有运气。
  • 也为我....对于捆绑中的文件“x.js”,请确保文件夹中没有“x.min.js”文件,否则尽管您已删除缩小转换 .. 捆绑将提供“预”缩小文件,例如如果你有 'angular.js' 然后删除 'angular.min.js' ;-)
【解决方案2】:

您需要在 Global.asax 的 Application_Start 事件中注册上面创建的捆绑包,例如

protected void Application_Start()
{
 RegisterBundles(BundleTable.Bundles);
 // Other Code is removed for clarity
}

捆绑和缩小在调试模式下不起作用。因此,要启用此功能,您需要在 Global.asax 的 Application_Start 事件中添加以下代码行。 protected void Application_Start()

{
 BundleConfig.RegisterBundles(BundleTable.Bundles);
 //Enabling Bundling and Minification
 BundleTable.EnableOptimizations = true; 
 // Other Code is removed for clarity
}

【讨论】:

    猜你喜欢
    • 2012-06-28
    • 1970-01-01
    • 2013-10-22
    • 2013-02-01
    • 2023-03-03
    • 2012-01-29
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    相关资源
    最近更新 更多