Visual Studio 2022 MVC 对象引用错误 @Script.Render(~/bundles/bootstrap)

Visual Studio 2022 MVC Object Reference Error with @Script.Render(~/bundles/bootstrap)

我是一名在 MVC 中从事小组项目的初学者大学生。在我们最近的代码更新之前,事情一直相对顺利。

在浏览器中(本地)测试应用程序时,应用程序抛出此错误:

Object Reference Error

堆栈跟踪:

[NullReferenceException: Object reference not set to an instance of an object.]
   Microsoft.Ajax.Utilities.JSParser.ParseObjectLiteralProperty(Boolean isBindingPattern) +944
   Microsoft.Ajax.Utilities.JSParser.ParseObjectLiteral(Boolean isBindingPattern) +164
   Microsoft.Ajax.Utilities.JSParser.ParseLeftHandSideExpression(Boolean isMinus) +2591
   Microsoft.Ajax.Utilities.JSParser.ParseUnaryExpression(Boolean& isLeftHandSideExpr, Boolean isMinus) +1090
   Microsoft.Ajax.Utilities.JSParser.ParseExpressionList(JSToken terminator) +408
   Microsoft.Ajax.Utilities.JSParser.ParseMemberExpression(AstNode expression, List`1 newContexts) +106
   Microsoft.Ajax.Utilities.JSParser.ParseLeftHandSideExpression(Boolean isMinus) +3074
   Microsoft.Ajax.Utilities.JSParser.ParseUnaryExpression(Boolean& isLeftHandSideExpr, Boolean isMinus) +1090
   Microsoft.Ajax.Utilities.JSParser.ParseExpressionStatement(Boolean fSourceElement) +41
   Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean fSourceElement, Boolean skipImportantComment) +1474
   Microsoft.Ajax.Utilities.JSParser.ParseBlock() +147
   Microsoft.Ajax.Utilities.JSParser.ParseArrowFunction(AstNode parameters) +200
   Microsoft.Ajax.Utilities.JSParser.ParseLeftHandSideExpression(Boolean isMinus) +3057
   Microsoft.Ajax.Utilities.JSParser.ParseUnaryExpression(Boolean& isLeftHandSideExpr, Boolean isMinus) +1090
   Microsoft.Ajax.Utilities.JSParser.ParseExpressionList(JSToken terminator) +408
   Microsoft.Ajax.Utilities.JSParser.ParseMemberExpression(AstNode expression, List`1 newContexts) +106
   Microsoft.Ajax.Utilities.JSParser.ParseLeftHandSideExpression(Boolean isMinus) +3074
   Microsoft.Ajax.Utilities.JSParser.ParseUnaryExpression(Boolean& isLeftHandSideExpr, Boolean isMinus) +1090
   Microsoft.Ajax.Utilities.JSParser.ParseExpressionStatement(Boolean fSourceElement) +41
   Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean fSourceElement, Boolean skipImportantComment) +1474
   Microsoft.Ajax.Utilities.JSParser.ParseBlock() +147
   Microsoft.Ajax.Utilities.JSParser.ParseIfStatement() +528
   Microsoft.Ajax.Utilities.JSParser.ParseFunctionBody(Block body) +43
   Microsoft.Ajax.Utilities.JSParser.ParseFunction(FunctionType functionType, Context fncCtx) +1083
   Microsoft.Ajax.Utilities.JSParser.ParseObjectLiteralProperty(Boolean isBindingPattern) +1168
   Microsoft.Ajax.Utilities.JSParser.ParseObjectLiteral(Boolean isBindingPattern) +164
   Microsoft.Ajax.Utilities.JSParser.ParseLeftHandSideExpression(Boolean isMinus) +2591
   Microsoft.Ajax.Utilities.JSParser.ParseUnaryExpression(Boolean& isLeftHandSideExpr, Boolean isMinus) +1090
   Microsoft.Ajax.Utilities.JSParser.ParseVarDecl(JSToken inToken) +407
   Microsoft.Ajax.Utilities.JSParser.ParseVariableStatement() +586
   Microsoft.Ajax.Utilities.JSParser.ParseFunctionBody(Block body) +43
   Microsoft.Ajax.Utilities.JSParser.ParseFunction(FunctionType functionType, Context fncCtx) +1083
   Microsoft.Ajax.Utilities.JSParser.ParseLeftHandSideExpression(Boolean isMinus) +2626
   Microsoft.Ajax.Utilities.JSParser.ParseUnaryExpression(Boolean& isLeftHandSideExpr, Boolean isMinus) +1090
   Microsoft.Ajax.Utilities.JSParser.ParseLeftHandSideExpression(Boolean isMinus) +2329
   Microsoft.Ajax.Utilities.JSParser.ParseUnaryExpression(Boolean& isLeftHandSideExpr, Boolean isMinus) +1090
   Microsoft.Ajax.Utilities.JSParser.ParseExpressionList(JSToken terminator) +408
   Microsoft.Ajax.Utilities.JSParser.ParseMemberExpression(AstNode expression, List`1 newContexts) +106
   Microsoft.Ajax.Utilities.JSParser.ParseLeftHandSideExpression(Boolean isMinus) +3074
   Microsoft.Ajax.Utilities.JSParser.ParseUnaryExpression(Boolean& isLeftHandSideExpr, Boolean isMinus) +1090
   Microsoft.Ajax.Utilities.JSParser.ParseExpressionStatement(Boolean fSourceElement) +41
   Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean fSourceElement, Boolean skipImportantComment) +1474
   Microsoft.Ajax.Utilities.JSParser.ParseStatements(Block block) +76
   Microsoft.Ajax.Utilities.JSParser.InternalParse() +848
   Microsoft.Ajax.Utilities.Minifier.MinifyJavaScript(String source, CodeSettings codeSettings) +805
   System.Web.Optimization.JsMinify.Process(BundleContext context, BundleResponse response) +115
   System.Web.Optimization.Bundle.ApplyTransforms(BundleContext context, String bundleContent, IEnumerable`1 bundleFiles) +325
   System.Web.Optimization.Bundle.GetBundleResponse(BundleContext context) +59
   System.Web.Optimization.BundleResolver.GetBundleContents(String virtualPath) +213
   System.Web.Optimization.AssetManager.DeterminePathsToRender(IEnumerable`1 assets) +421
   System.Web.Optimization.AssetManager.RenderExplicit(String tagFormat, String[] paths) +34
   ASP._Page_Views_Shared__Layout_cshtml.Execute() in C:\Users\tyler_sfm3fox\Desktop\Capstone\InTurn\InTurn\Views\Shared\_Layout.cshtml:46
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +252
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +148
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +122
   System.Web.WebPages.<>c__DisplayClass40_0.<RenderPageCore>b__0(TextWriter writer) +309
   System.Web.WebPages.WebPageBase.Write(HelperResult result) +108
   System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +89
   System.Web.WebPages.WebPageBase.PopContext() +310
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +378
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +91
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +795
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +81
   System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult) +188
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +38
   System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +73
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +52
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38
   System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +43
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +73
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +431
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +75
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +158

我认为这是 BundleConfig.cs 文件或 Bootstrap.js 文件的问题。我不完全确定在哪里或如何寻找根本原因。

我在这个网站上浏览过类似的问题,一个常见的解决方案是更改

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

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

这阻止了异常的发生,但是 bootstrap 元素消失了。

Before

After

我一直在用头撞墙试图找出发生了什么事。

所以团队中一定有人更新了 Bootstrap 和 jQuery。我们的项目模板不喜欢那样。我继续降级到 ASP.Net Web 应用程序 (.net Framework) 模板附带的默认 Bootstrap 和 jQuery 版本并解决了问题。

虽然我很想知道如何在不降级的情况下解决这个问题。

不过很高兴地说,项目已经启动并且 运行。

这个问题只发生在 VS 2022 上,但 VS 2019 没有问题。