如果没有将EnableOptimization设置为true,则JS包不会呈现

Sta*_*tan 21 c# asp.net asp.net-mvc asp.net-mvc-4

我不知道我做错了什么,但它可能是MVC4中的一个错误.我想知道如何解决这个问题?

工作方案

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        ScriptBundle scriptBundle = new ScriptBundle("~/js");
        string[] scriptArray =
        {
            "~/content/plugins/jquery/jquery-1.8.2.min.js",
            "~/content/plugins/jquery/jquery-ui-1.9.0.min.js",
            "~/content/plugins/jquery/jquery.validate.min.js",
            "~/content/plugins/jquery/jquery.validate.unobtrusive.min.js",
            "~/content/plugins/bootstrap/js/bootstrap.min.js",
        };
        scriptBundle.Include(scriptArray);
        scriptBundle.IncludeDirectory("~/content/js", "*.js");
        bundles.Add(scriptBundle);

        BundleTable.EnableOptimizations = true;
    }
}
Run Code Online (Sandbox Code Playgroud)

@Scripts.Render("~/js")
转换为(例如IT工作!)
<script src="/js?v=VeCPNK561DZp34yjmWbLrNM35Kf6gaNDl0xsMMC25BQ1"></script>

工作场景不是那么多

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        ScriptBundle scriptBundle = new ScriptBundle("~/js");
        string[] scriptArray =
        {
            "~/content/plugins/jquery/jquery-1.8.2.min.js",
            "~/content/plugins/jquery/jquery-ui-1.9.0.min.js",
            "~/content/plugins/jquery/jquery.validate.min.js",
            "~/content/plugins/jquery/jquery.validate.unobtrusive.min.js",
            "~/content/plugins/bootstrap/js/bootstrap.min.js",
        };
        scriptBundle.Include(scriptArray);
        scriptBundle.IncludeDirectory("~/content/js", "*.js");
        bundles.Add(scriptBundle);

        // BundleTable.EnableOptimizations = true; // I could set it to 'false' for same result, it's false by default
    }
}
Run Code Online (Sandbox Code Playgroud)

@Scripts.Render("~/js")
转换为(例如,它不工作!)
(nothing, couple of empty break lines)

Fen*_*ton 30

如果我的理解是正确的,您应该使用"非最小"JavaScript文件定义您的包.当您启用优化时,它将为您分配min文件的非min文件:

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        ScriptBundle scriptBundle = new ScriptBundle("~/js");
        string[] scriptArray =
        {
            "~/content/plugins/jquery/jquery-1.8.2.js",
            "~/content/plugins/jquery/jquery-ui-1.9.0.js",
            "~/content/plugins/jquery/jquery.validate.js",
            "~/content/plugins/jquery/jquery.validate.unobtrusive.js",
            "~/content/plugins/bootstrap/js/bootstrap.js",
        };
        scriptBundle.Include(scriptArray);
        scriptBundle.IncludeDirectory("~/content/js", "*.js");
        bundles.Add(scriptBundle);
    }
}
Run Code Online (Sandbox Code Playgroud)

调试时优化设置为false,但默认情况下在发布模式下为true.

  • 您可以将.min文件保留在那里,但一定要添加`bundles.IgnoreList.Clear();`否则它们将被忽略. (3认同)

小智 8

是的,它与*.min.js等文件无法正常工作.

"如果你向你的包中添加一个名称以.min.js结尾的文件(就像我一样)并且未启用优化(例如,在web.config中将debug设置为true并且尚未设置BundleTable.EnableOptimizations为了真实),这个文件将被忽略(即你的html中不会为它生成脚本包含)."

在这里,您可以阅读完整的原始回复:http: //blog.degree.no/2013/06/javascript-file-missing-resource-bundle-asp-net-web-optimization-framework/