【问题标题】:asp.net mvc 5 bundle jquery not workingasp.net mvc 5 捆绑 jquery 不工作
【发布时间】:2018-12-25 22:34:51
【问题描述】:

我的应用程序可以调试并在本地调用 jquery 3.3.1 但在发布应用程序后一切正常 javascrips 和 css 但我的下拉菜单需要 jquery 不起作用。我想在视图页面中使用此捆绑包,不要在布局中使用请帮助我... 我的 budlens 课程包括

    public static void Registerbundles(BundleCollection bundles)

     {
       bundles.IgnoreList.Clear();

        //jquery
       bundles.Add(new ScriptBundle("~/bundles/jquery").Include("~/Scripts/jquery-{version}.js")
                                                       .Include("~/Scripts/jquery-3.3.1.js")
                                                       .Include("~/Scripts/jquery-3.3.1.min.js")
                                                       .Include("~/Scripts/jquery-3.3.1.slim.js")
                                                       .Include("~/Scripts/jquery-3.3.1.slim.min.js")
                                                       .Include("~/Scripts/jquery-3.3.1.slim.min.map")
                                                       .Include("~/Scripts/jquery-3.3.1-vsdoc.js"));

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

       // java script
       bundles.Add(new ScriptBundle("~/bundles/scripts").Include("~/Scripts/bootstrap.js")
                                                        .Include("~/Scripts/bootstrap.min.js")
                                                        .Include("~/Scripts/jalaali.js")
                                                        .Include("~/Scripts/jquery.Bootstrap-PersianDateTimePicker.js"));


        // bootsrtap CSS
        //bundles.Add(new StyleBundle("~/bundles/css").IncludeDirectory("~/Content/", "*.css", true));


       bundles.Add(new StyleBundle("~/bundles/css").Include("~/Content/bootstrap.min.css",
                                                            "~/Content/bootstrap.theme.min.css" ,                           
                                                            "~/Content/site.css",
                                                            "~/Content/bootstrap.css",
                                                            "~/Content/bootstrap.theme.css",
                                                            "~/Content/jquery.Bootstrap-PersianDateTimePicker.css"
                                                                                         ));


        BundleTable.EnableOptimizations = true;

    }

并在视图中调用...

<script src="~/bundles/jQuery" type="text/javascript"></script>

<script src="~/bundles/scripts" type="text/javascript"></script>

<link href="~/bundles/css" rel="stylesheet" />

【问题讨论】:

  • 你确定捆绑使用 src/href 这样的模式吗?通常它使用System.Web.Optimization.Scripts.Render("~/bundles/jQuery")(用于JS文件)或System.Web.Optimization.Styles.Render("~/bundles/css")(用于CSS文件)。
  • 感谢您的回复。我检查了这个命令,但没有任何改变...
  • 使用浏览器的开发工具(F12),检查控制台和网络选项卡。如果您有任何错误,请将它们作为详细信息包含在内。即使您使用Render(),样式表和JS脚本仍然无法正常工作?
  • 即使我使用渲染仍然无法正常工作,我在 consol 中有 2 个错误
    1 lh5.googleusercontent.com/-b0-k99FZlyE/AAAAAAAAAAI/AAAAAAAAAAA/… 加载资源失败:net::ERR_NAME_NOT_RESOLVED
    2 加载资源失败:服务器响应状态为 404(未找到)
    3 加载资源失败:服务器响应状态为 404(未找到)@TetsuyaYamamoto
  • 通常我使用*这样的通配符:bundles.Add(new ScriptBundle("~/bundles/jquery").Include("~/Scripts/jquery-*")。它将包含所有 jQuery 脚本,并且适用于本地和生产机器。

标签: asp.net html asp.net-mvc-5


【解决方案1】:

视图路径可能会导致问题。尝试像这样重新排列视图路径

<script src="~/bundles/scripts/jquery-3.3.1.js" type="text/javascript"></script>

在浏览器中调试 JavaScript 之后,在这里放置断点并找到您的解决方案。 在 chrome 中调试 JavaScript(https://www.youtube.com/watch?v=KbEx0s06VLs&t=4s)。

【讨论】:

    【解决方案2】:

    - 字符可能会导致该问题。尝试重命名您的文件,例如,从 "~/Scripts/jquery-3.3.1.js""~/Scripts/jquery.3.3.1.js"。将此更改应用于所有捆绑资源。

    【讨论】:

    • 感谢回复。我将字符(-)改为指向(。)但仍然无法工作@remus
    • 我在本地使用“~/Scripts/jquery-3.3.1.js”工作正常,但发布后我使用 src="~/bundles/jQuery" 无法正常工作。 @Remus
    【解决方案3】:

    我创建了新的捆绑包

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

    我在母版页上调用它

    @Scripts.Render("~/bundles/jqueSlim")
    

    使用 System.Web.Optimization

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      • 2017-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      相关资源
      最近更新 更多