当我尝试将我的ASP.net MVC Web应用程序发布到我的测试服务器IIS时,我遇到了一些奇怪的问题。我已经决定这可能与捆绑有关。在我的应用程序中,我使用jquery数据表来显示从db到用户的实时数据。当我通过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)
答案 0 :(得分:2)
您已将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脚本