只允许在mvc4中捆绑不缩小

时间:2013-07-15 07:13:26

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

我正在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" />

任何人都可以告诉我我在哪里做错了。 是否可以仅启用捆绑包?

由于 阿苏

2 个答案:

答案 0 :(得分:0)

内置无配置/选项,允许您在不缩小的情况下启用捆绑。

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

因此,为了在没有Minification的情况下启用捆绑,我们可以尝试这样做:

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

答案 1 :(得分:0)

您需要在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
}