我有一个奇怪的问题与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'文件?


当前回答

捆扎机有很多好处,看看这个页面,但是:

微软MVC4平台认为每个脚本或样式包至少都有缩小版和非缩小版(其他文件如Debug和vsdoc也可用)。在只有一个文件的情况下,我们会遇到麻烦。

您可以在web中更改调试状态。配置文件永久处理输出:

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

查看输出变化!文件过滤发生变化。为了达到这个目的,我们必须改变忽略大小写过滤,这会改变应用程序逻辑!

其他回答

我最初发布的解决方案是有问题的(是一个肮脏的黑客)。正如许多评论者指出的那样,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代码时再修正它

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中。我知道这不是正确的方法,但这只是一个临时的解决办法。谢谢大家,编码愉快。

要呈现*.min.js文件,必须禁用BundleTable。EnableOptimizations,这是一个应用于所有捆绑包的全局设置。

如果您只想为特定的包启用优化,您可以创建自己的ScriptBundle类型,在枚举包中的文件时临时启用优化。

public class OptimizedScriptBundle : ScriptBundle
{
    public OptimizedScriptBundle(string virtualPath)
        : base(virtualPath)
    {
    }

    public OptimizedScriptBundle(string virtualPath, string cdnPath)
        : base(virtualPath, cdnPath)
    {
    }

    public override IEnumerable<BundleFile> EnumerateFiles(BundleContext context)
    {
        if (context.EnableOptimizations)
            return base.EnumerateFiles(context);
        try
        {
            context.EnableOptimizations = true;
            return base.EnumerateFiles(context);
        }
        finally
        {
            context.EnableOptimizations = false;
        }
    }
}

对于始终应该使用最小化文件的包,无论是否存在非最小化文件,都应使用OptimizedScriptBundle而不是ScriptBundle。

ASP的Kendo UI示例。NET MVC,它仅作为一个缩小文件的集合分发。

bundles.Add(new OptimizedScriptBundle("~/bundles/kendo")
        .Include(
            "~/Scripts/kendo/kendo.all.*",
            "~/Scripts/kendo/kendo.aspnetmvc.*",
            "~/Scripts/kendo/cultures/kendo.*",
            "~/Scripts/kendo/messages/kendo.*"));