我有一个奇怪的问题与mvc4捆绑不包括文件扩展名.min.js

在BundleConfig类中,我声明

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/Scripts/jquery")
        .Include("~/Scripts/jquery-1.8.0.js")
        .Include("~/Scripts/jquery.tmpl.min.js"));            
}

在我看来,我宣布

<html>
    <head>
    @Scripts.Render("~/Scripts/jquery")
    </head><body>test</body>
</html>

当它渲染时,它只渲染

<html>
    <head>
         <script src="/Scripts/jquery-1.8.0.js"></script>
    </head>
    <body>test</body>
</html>

如果我将jquery.tmpl.min.js重命名为jquery.tmpl.js(并相应地更新包中的路径),两个脚本都将正确呈现。

是否有一些配置设置导致它忽略'.min.js'文件?


当前回答

人 我会保持简单,直到微软采取行动

试试这个

在RegisterBundles中为Kendo创建这个bundle

bundles.Add(new StyleBundle("~/Content/kendo/css").Include(
                    "~/Content/kendo/2012.3.1121/kendo.common.min.css",
                     "~/Content/kendo/2012.3.1121/kendo.dataviz.min.css",
                      "~/Content/kendo/2012.3.1121/kendo.blueopal.min.css"
 ));

在_Layout。CSHTML放了这个:

@if (HttpContext.Current.IsDebuggingEnabled)
 { 
<link href="@Url.Content("~/Content/kendo/2012.3.1121/kendo.common.min.css")"      
rel="stylesheet"  type="text/css" />
<link href="@Url.Content("~/Content/kendo/2012.3.1121/kendo.dataviz.min.css")" 
rel="stylesheet" type="text/css" />   
<link href="@Url.Content("~/Content/kendo/2012.3.1121/kendo.blueopal.min.css")"    
rel="stylesheet" type="text/css" />
 }
 else
 {
     @Styles.Render("~/Content/kendo/css")   
 }

通过这种方式,我们在生产中得到了最好的包,在调试中得到了引用

当微软修正他们的MVC 4代码时再修正它

其他回答

I have found a good solution that works at least in MVC5, you can just use Bundle instead of ScriptBundle. It does not have the smart behavior of ScriptBundle that we don't like (ignoring .min, etc.) in this case. In my solution I use Bundle for 3d party scripts with .min and .map files and I use ScriptBundle for our code. I have not noticed any drawbacks of doing it. To make it work this way you will need to add original file e.g. angular.js, and it will load angular.js in debug and it will bundle the angular.min.js in release mode.

一个简单的方法就是重命名。min文件,比如你有abc.min.css,而不是把这个文件重命名为abc_min.css,然后把它添加到你的bundle中。我知道这不是正确的方法,但这只是一个临时的解决办法。谢谢大家,编码愉快。

我最初发布的解决方案是有问题的(是一个肮脏的黑客)。正如许多评论者指出的那样,Microsoft.AspNet.Web.Optimization包中的调整行为已经改变,并且调整不再有效。现在我无法在1.1.3版本的包中重现这个问题。

请参阅System.Web.Optimization.BundleCollection的源代码(例如,您可以使用dotPeek),以便更好地理解您将要做的事情。 请阅读Max Shmelev的回答。

最初的回答:

要么将.min.js重命名为.js,要么做类似

    public static void AddDefaultIgnorePatterns(IgnoreList ignoreList)
    {
        if (ignoreList == null)
            throw new ArgumentNullException("ignoreList");
        ignoreList.Ignore("*.intellisense.js");
        ignoreList.Ignore("*-vsdoc.js");
        ignoreList.Ignore("*.debug.js", OptimizationMode.WhenEnabled);
        //ignoreList.Ignore("*.min.js", OptimizationMode.WhenDisabled);
        ignoreList.Ignore("*.min.css", OptimizationMode.WhenDisabled);
    }

    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.IgnoreList.Clear();
        AddDefaultIgnorePatterns(bundles.IgnoreList);
        //NOTE: it's bundles.DirectoryFilter in Microsoft.AspNet.Web.Optimization.1.1.3 and not bundles.IgnoreList

        //...your code
     }

人 我会保持简单,直到微软采取行动

试试这个

在RegisterBundles中为Kendo创建这个bundle

bundles.Add(new StyleBundle("~/Content/kendo/css").Include(
                    "~/Content/kendo/2012.3.1121/kendo.common.min.css",
                     "~/Content/kendo/2012.3.1121/kendo.dataviz.min.css",
                      "~/Content/kendo/2012.3.1121/kendo.blueopal.min.css"
 ));

在_Layout。CSHTML放了这个:

@if (HttpContext.Current.IsDebuggingEnabled)
 { 
<link href="@Url.Content("~/Content/kendo/2012.3.1121/kendo.common.min.css")"      
rel="stylesheet"  type="text/css" />
<link href="@Url.Content("~/Content/kendo/2012.3.1121/kendo.dataviz.min.css")" 
rel="stylesheet" type="text/css" />   
<link href="@Url.Content("~/Content/kendo/2012.3.1121/kendo.blueopal.min.css")"    
rel="stylesheet" type="text/css" />
 }
 else
 {
     @Styles.Render("~/Content/kendo/css")   
 }

通过这种方式,我们在生产中得到了最好的包,在调试中得到了引用

当微软修正他们的MVC 4代码时再修正它

微软暗示了以下行为(我更喜欢在我的项目中遵循它):

短的版本

在你的项目中,一个脚本的调试版和缩小版都在同一个文件夹下: script.js script.min.js 在代码中只向包中添加script.js。

因此,你会自动在DEBUG模式下包含script.js,在RELEASE模式下包含script.min.js。

长版本

你也可以有。debug.js版本。在这种情况下,文件包含在DEBUG的以下优先级中:

script.debug.js script.js

发布:

script.min.js script.js

note

顺便说一下,在MVC4中使用.min版本脚本的唯一原因是缩小版本不能自动处理。例如下面的代码不能被自动混淆:

if (DEBUG) console.log("Debug message");

在所有其他情况下,您可以只使用脚本的调试版本。