c# - 如果 EnableOptimization 设置为 true,则 JS 包不会呈现

标签 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;
    }
}

@Scripts.Render("~/js")
转换为(例如它有效!)
<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
    }
}

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

最佳答案

如果我的理解是正确的,您应该使用“非最小”JavaScript 文件定义您的包。当您启用优化时,它会为您将非最小文件交换为最小文件:

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);
    }
}

优化在调试时设置为 false,但在 Release模式下默认为 true。

关于c# - 如果 EnableOptimization 设置为 true,则 JS 包不会呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13458642/

相关文章:

c# - 发送大量邮件时 SmtpClient.SendMailAsync 方法挂起

c# - 获取 DatagridviewComboBoxCell 的 SelectedIndex

c# - 网络开发新手。 ASP.NET 还是 Django?

asp.net-mvc - 是否可以创建一个独立的局部 View "widget"?

c# - 如何在ASP.NET MVC 2应用程序中实现向导类型的页面导航?

c# - 如何在 C# 中将默认的 FlushMode 更改为 Commit?

c# - 何时在 C# 中使用 Shift 运算符 << >>?

c# - 处理 ASP.NET Core 1.0 上的大文件上传

c# - 为什么net Framework 4.5 azure部署忽略应用程序服务连接字符串

ajax - 替换 Mvc 部分 View 后的 select2 jquery 不再起作用