我有一个奇怪的问题与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代码时再修正它

其他回答

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

要呈现*.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.*"));

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

试试这个

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

对于我的案例,我使用的是(太棒了!)Knockout.js库作为调试/非版本:

"~/Scripts/knockout-{Version}.js"
"~/Scripts/knockout-{Version}.Debug.js"

为了做到这一点,我在我的Bundle中包含了“Knockout-{Version}.js”(非调试),并获得了.debug。js文件在调试模式。