【问题标题】:Problems with style of Jquery Datatable at publishing (possibly bundling issue)发布时 Jquery Datatable 样式的问题(可能是捆绑问题)
【发布时间】:2015-02-26 14:12:35
【问题描述】:

当我尝试将我的 ASP.net MVC Web 应用程序发布到我的测试服务器 IIS 时,我遇到了一些奇怪的问题。我已经决定这可能与捆绑有关。在我的应用程序中,我使用 jquery 数据表将实时数据从数据库显示给用户。当我通过 vs2013 调试器启动应用程序时,应用程序看起来和运行良好。但是,当我发布到测试服务器的 IIS 时,数据表的样式似乎消失了(表功能似乎仍在工作)。

我已添加:

BundleTable.EnableOptimizations = true;

并像这样更改了 web.config

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

这是我的捆绑配置:

public static void RegisterBundles(BundleCollection bundles)
    {
        BundleTable.EnableOptimizations = true;

        bundles.Add(new ScriptBundle("~/bundles/CustomJqueryBundle").Include(
                    "~/Scripts/jquery-{version}.js",
                    "~/Scripts/jquery-ui-{version}.js",
                    "~/Scripts/jquery.unobtrusive*",
                    "~/Scripts/jquery.validate"));

        bundles.Add(new ScriptBundle("~/bundles/CustomJqueryDataTable_Scripts").Include(
                    "~/Content/DataTables-1.10.5/DataTables-1.10.5/media/js/jquery.dataTables.js",
                    "~/Content/DataTables-1.10.5/DataTables-1.10.5/extensions/Responsive/js/dataTables.responsive.js"));

        bundles.Add(new ScriptBundle("~/bundles/CustomJqueryDataTable_Styles").Include(
                    "~/Content/DataTables-1.10.5/DataTables-1.10.5/media/css/jquery.dataTables.css",
                    "~/Content/DataTables-1.10.5/DataTables-1.10.5/extensions/Responsive/css/dataTables.responsive.css"));

        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));

        // Use the development version of Modernizr to develop with and learn from. Then, when you're
        // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.js",
                  "~/Scripts/respond.js"));

        bundles.Add(new StyleBundle("~/Content/css").Include(
                  "~/Content/bootstrap.css",
                  "~/Content/site.css"));
    }

这是我在 _layout 中呈现数据表脚本的地方

@Scripts.Render("~/bundles/CustomJqueryBundle", "~/bundles/bootstrap", "~/bundles/CustomJqueryDataTable_Scripts")
@Styles.Render("~/bundles/CustomJqueryDataTable_Styles")
@RenderSection("scripts", required: false)

另外值得注意的是,mvc 默认样式似乎仍然可以正常工作

通过调试器(BundleTable.EnableOptimizations = true 注释掉)

发布时(或通过带有 BundleTable.EnableOptimizations = true 的调试器未注释掉)

我也刚刚尝试执行以下操作,而不是使用 scripts.render 并且应用程序使用样式发布,证明这是一个捆绑问题:

<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/Scripts/bootstrap.js"></script>
<script src="~/Scripts/respond.js"></script>
<script src="~/Content/DataTables-1.10.5/DataTables-1.10.5/media/js/jquery.dataTables.js"></script>
<link href="~/Content/DataTables-1.10.5/DataTables-1.10.5/media/css/jquery.dataTables.css" rel="stylesheet" />
<link href="~/Content/DataTables-1.10.5/DataTables-1.10.5/extensions/Responsive/css/dataTables.responsive.css" rel="stylesheet" />
<script src="~/Content/DataTables-1.10.5/DataTables-1.10.5/extensions/Responsive/js/dataTables.responsive.js"></script>


@RenderSection("scripts", required: false)

【问题讨论】:

    标签: asp.net-mvc css datatables jquery-datatables bundling-and-minification


    【解决方案1】:

    您已将 CSS 捆绑为 JS 脚本 (ScriptBundle) 而不是 CSS (StyleBundle) - 您需要:

     bundles.Add(new StyleBundle("~/bundles/CustomJqueryDataTable_Styles").Include(
                    "~/Content/DataTables-1.10.5/DataTables-1.10.5/media/css/jquery.dataTables.css",
                    "~/Content/DataTables-1.10.5/DataTables-1.10.5/extensions/Responsive/css/dataTables.responsive.css"));
    

    所以在优化过程中会被当作CSS而不是JS脚本来处理

    【讨论】:

    • 哦,天哪,我很尴尬,但非常感谢你发现这一点。我开始发疯了。
    猜你喜欢
    • 2014-01-31
    • 2018-03-19
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    相关资源
    最近更新 更多