【问题标题】:MVC 5 @Scripts.Render("~/bundles/bootstrap") error 'Object reference not set to an instance of an object.'MVC 5 @Scripts.Render("~/bundles/bootstrap") 错误“对象引用未设置为对象的实例。”
【发布时间】:2021-09-01 15:44:43
【问题描述】:

_Layout.cshtml 文件在尝试呈现引导程序 5 脚本包时失败(适用于引导程序的早期版本)

Bundle.Config File

_Layout Debugging

Scripts Folder

【问题讨论】:

  • 尝试将“new ScriptBundle”更改为“new Bundle”。
  • 我遇到了一个非常相似的问题...我的菜单不再重新渲染...我尝试了上面建议的更改,以及下面答案中建议的更改...没有运气
  • @ManojSalvi 您应该将此作为答案。它对我有用。
  • @jewishspiderweb 我已按照您的要求发布了我的建议作为答案,这样它也可以帮助面临同样问题的其他人。如果它对你有用,你可以检查并投票。

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


【解决方案1】:

尝试在 BundleConfig 中的“RegisterBundles”中将“new ScriptBundle”更改为“new Bundle”:

bundles.Add(new Bundle("~/bundles/bootstrap").Include("~/Scripts/bootstrap.js"));

【讨论】:

  • 这对我来说是解决办法。
  • 这对我也有用。
  • 也为我工作。有人知道为什么这个答案有效吗? “ScriptBundle”工作时的突然变化,然后从一个构建到另一个构建,它停止工作,然后不得不将其更改为“Bundle”......我发现这很奇怪! ://
【解决方案2】:

通过 Nuget 将引导程序更新到最新版本 (v5.0.2) 后,我遇到了同样的问题。我在上面看到了一个关于将 ScriptBundle 更改为 Bundle with minified js 的答案。这不是这个问题的真正解决方案。脚本捆绑仅用于在发布模式下缩小脚本。我们需要调试模式下的未缩小版本进行调试。所以如果你已经在使用 minified js,你不需要将它添加到 bundleconfig 中。

我尝试将 bootstrap.js 和 bootstrap.bundle.js 捆绑在一起,但在脚本渲染阶段遇到了同样的异常。

我修剪了很长的堆栈跟踪如下:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=WebGrease
  StackTrace:
   at Microsoft.Ajax.Utilities.JSParser.ParseObjectLiteralProperty(Boolean isBindingPattern)
   at Microsoft.Ajax.Utilities.JSParser.ParseObjectLiteral(Boolean isBindingPattern)
   at Microsoft.Ajax.Utilities.JSParser.ParseLeftHandSideExpression(Boolean isMinus)
   at Microsoft.Ajax.Utilities.JSParser.ParseUnaryExpression(Boolean& isLeftHandSideExpr, Boolean isMinus)
   at Microsoft.Ajax.Utilities.JSParser.ParseExpressionList(JSToken terminator)
   at Microsoft.Ajax.Utilities.JSParser.ParseMemberExpression(AstNode expression, List`1 newContexts)
   at Microsoft.Ajax.Utilities.JSParser.ParseLeftHandSideExpression(Boolean isMinus)
   at Microsoft.Ajax.Utilities.JSParser.ParseUnaryExpression(Boolean& isLeftHandSideExpr, Boolean isMinus)
   at Microsoft.Ajax.Utilities.JSParser.ParseExpressionStatement(Boolean fSourceElement)
   at Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean fSourceElement, Boolean skipImportantComment)
   at Microsoft.Ajax.Utilities.JSParser.ParseBlock()
   at Microsoft.Ajax.Utilities.JSParser.ParseArrowFunction(AstNode parameters)
   at Microsoft.Ajax.Utilities.JSParser.ParseLeftHandSideExpression(Boolean isMinus)
   at Microsoft.Ajax.Utilities.JSParser.ParseUnaryExpression(Boolean& isLeftHandSideExpr, Boolean isMinus)

我认为新的 bootstrap.js 或 bootstrap.bundle.js 中存在 js 格式问题,导致解析器崩溃。

【讨论】:

    【解决方案3】:

    正如@Manoj Salvi 评论的那样,Bootstrap 5 可以成功捆绑:

    bundles.Add(new Bundle("~/scripts/core").Include("~/content/scripts/bootstrap.bundle.min.js"));
    

    【讨论】:

      【解决方案4】:

      公共类 BundleConfig { // Paketleme hakkında daha fazla bilgi için lütfen https://go.microsoft.com/fwlink/?LinkId=301862 adresini ziyaret edin。 公共静态无效RegisterBundles(BundleCollection捆绑) { bundles.Add(new ScriptBundle("~/bundles/jquery").Include( "~/Scripts/jquery-{version}.js"));

              bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                          "~/Scripts/jquery.validate*"));
      
              // Geliştirme yapmak ve öğrenme kaynağı olarak yararlanmak için Modernizr uygulamasının geliştirme sürümünü kullanın. Ardından
              // üretim için hazır. https://modernizr.com adresinde derleme aracını kullanarak yalnızca ihtiyacınız olan testleri seçin.
              bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                          "~/Scripts/modernizr-*"));
      
              bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                        "~/Scripts/bootstrap.js"));
      
              bundles.Add(new StyleBundle("~/Content/css").Include(
                        "~/Content/bootstrap.css",
                        "~/Content/site.css"));
      
              bundles.Add(new Bundle("~/bundles/bootstrap").Include(
                         "~/Scripts/bootstrap.js",
                         "~/Scripts/respond.js",
                         "~/Content/site.css"));
          }
      }
      

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      【解决方案5】:

      尝试降级您的引导程序版本。我在 5.1 上,我降级到 4.6,现在它可以工作了。

      【讨论】:

      • 我不认为 OP 正在寻求降级,但有提到的版本的解决方案
      猜你喜欢
      • 1970-01-01
      • 2015-06-12
      • 2015-09-27
      • 2013-01-19
      • 2011-06-21
      • 2011-01-30
      • 1970-01-01
      • 2013-07-26
      • 2011-07-09
      相关资源
      最近更新 更多