【问题标题】:C# ASP.NET MVC scripts being loaded in twiceC# ASP.NET MVC 脚本被加载两次
【发布时间】:2020-02-28 02:59:39
【问题描述】:

我有一个 C# ASP.NET MVC 应用程序加载脚本两次并在控制台中给我错误。第一次加载脚本时,它们被正确加载,但第二次,它们会导致错误。

这是浏览器控制台的截图:

这是我的布局,这是唯一调用脚本的地方:

<head>
    <meta http-equiv="x-ua-compatible" content="ie=edge" charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="icon" href="png/favicon.png" id="favicon-i">

    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/other")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @Scripts.Render("~/bundles/picker")
</head>

这是我的捆绑包,并非所有捆绑包都在使用:

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 
// https://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/bootstrap.min.js"));

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                "~/Scripts/jquery-3.3.1.min.js",
                "~/Scripts/jquery-3.3.1.js"));

bundles.Add(new ScriptBundle("~/bundles/picker").Include(
                "~/Scripts/bootstrap-datepicker.js",
                "~/Scripts/bootstrap-datetimepicker.js",
                "~/Scripts/bootstrap-datetimepicker.min.js",
                "~/Scripts/bootstrap-multiselect.js"));

bundles.Add(new ScriptBundle("~/Scripts").Include(
                "~/Scripts/tether.min.js",
                "~/Scripts/tether.js",                 
                "~/Scripts/apexcharts.min.js",
                "~/Scripts/layout.js",
                "~/Scripts/moment.js",
                "~/Scripts/Chart.js"));

bundles.Add(new ScriptBundle("~/bundles/other").Include(
                "~/Scripts/tether.min.js",
                "~/Scripts/tether.js",
                "~/Scripts/apexcharts.min.js",
                "~/Scripts/layout.js",
                "~/Scripts/moment.js",
                "~/Scripts/Chart.js"));

bundles.Add(new StyleBundle("~/Content/css").Include( "~/Content/bootstrap.css", "~/Content/layout.css", "~/Content/Style.css", "~/Content/tether.min.css", "~/Content/bootstrap-datepicker.css", "~/Content/bootstrap-datetimepicker.css", "~/Content/bootstrap-multiselect.css"));

任何想法为什么会发生这种情况?

编辑:这就是发起者的样子:

【问题讨论】:

标签: c# asp.net-mvc


【解决方案1】:

查看 Chrome 开发工具中网络选项卡上的启动器列可以帮助跟踪文件的加载位置

【讨论】:

  • 看起来每个视图都在加载它们,是这样吗?
  • @GreenSaber 绝对。如果您使用的是局部视图并且其中有脚本定义,它们将在每个局部视图中加载一次。
  • 我正在使用调用视图的布局。脚本是从调用正确视图的布局中调用的。我刚刚再次检查,每次调用视图时都会调用脚本。我怎样才能阻止这种情况?
  • @GreenSaber 如果您正在调用操作以返回视图,则返回 PartialView 而不是 View - View 调用返回布局,而 PartialView 仅返回 .cshtml 文件
  • 当我进行更改时,我的网站仅显示部分视图的 html。没有 css、js 或 _Layout 中的任何内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-06
  • 2017-06-12
  • 1970-01-01
相关资源
最近更新 更多