Scripts.Render 抛出“对象未设置为对象的实例”错误

Fre*_*kyB 11 .net c# asp.net-mvc

我正在使用 Visual Studio 2019。

我有一个以前没有遇到过的问题 - 在视图中,这一行(只是这一行)抛出错误:

@Scripts.Render("~/scripts/scriptCommon")
Run Code Online (Sandbox Code Playgroud)

我检查了捆绑配置:

ScriptBundle scriptCommon = new ScriptBundle("~/scripts/scriptCommon");
      scriptCommon.Include("~/static/scripts/jquery-1.11.0.min.js");
      scriptCommon.Include("~/static/scripts/bootstrap.min.js");
Run Code Online (Sandbox Code Playgroud)

这两个文件都在文件系统上。

该捆绑包还添加到具有以下内容的捆绑包中:

bundles.Add(scriptCommon);
Run Code Online (Sandbox Code Playgroud)

(就像所有其他捆绑包一样)

我不明白为什么会出现这个错误?可能是什么原因?

简短的错误消息:

System.NullReferenceException: Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

堆栈跟踪:

[NullReferenceException: Object reference not set to an instance of an object.]
   Microsoft.Ajax.Utilities.ActivationObject.DefineField(INameDeclaration nameDecl, FunctionObject fieldValue) +20
   Microsoft.Ajax.Utilities.ActivationObject.DefineLexicalDeclarations() +92
   Microsoft.Ajax.Utilities.BlockScope.DeclareScope() +4
   Microsoft.Ajax.Utilities.ResolutionVisitor.CreateFields(ActivationObject scope) +35
   Microsoft.Ajax.Utilities.ResolutionVisitor.CreateFields(ActivationObject scope) +77
   Microsoft.Ajax.Utilities.ResolutionVisitor.CreateFields(ActivationObject scope) +77
   Microsoft.Ajax.Utilities.ResolutionVisitor.CreateFields(ActivationObject scope) +77
   Microsoft.Ajax.Utilities.ResolutionVisitor.Apply(AstNode node, ActivationObject scope, CodeSettings settings) +62
   Microsoft.Ajax.Utilities.JSParser.Parse(CodeSettings settings) +972
   Microsoft.Ajax.Utilities.Minifier.MinifyJavaScript(String source, CodeSettings codeSettings) +548
   System.Web.Optimization.JsMinify.Process(BundleContext context, BundleResponse response) +92
   System.Web.Optimization.Bundle.ApplyTransforms(BundleContext context, String bundleContent, IEnumerable`1 bundleFiles) +273
   System.Web.Optimization.Bundle.GenerateBundleResponse(BundleContext context) +141
   System.Web.Optimization.Bundle.GetBundleResponse(BundleContext context) +45
   System.Web.Optimization.BundleResolver.GetBundleContents(String virtualPath) +166
   System.Web.Optimization.AssetManager.EliminateDuplicatesAndResolveUrls(IEnumerable`1 refs) +284
   System.Web.Optimization.AssetManager.DeterminePathsToRender(IEnumerable`1 assets) +761
   System.Web.Optimization.AssetManager.RenderExplicit(String tagFormat, String[] paths) +35
   System.Web.Optimization.Scripts.RenderFormat(String tagFormat, String[] paths) +107
   System.Web.Optimization.Scripts.Render(String[] paths) +21
   ASP._Page_Views_CodeBase_CodeBaseTbl_cshtml.Execute() in D:\ROOT\GIT\Franjo_dev01\Solution\fCatEve\fCatEve\Views\CodeBase\CodeBaseTbl.cshtml:9
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +198
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +105
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +173
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9849569
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +50
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +163
Run Code Online (Sandbox Code Playgroud)

car*_*m85 20

试试这个,它在我的系统和项目中有效:

  1. 打开BundleConfig.cs。您应该在文件夹中找到它App_Start
  2. 然后更改特定脚本的捆绑@Scripts.Render("~/scripts/scriptCommon"),从ScriptBundle scriptCommon = new ScriptBundle("~/scripts/scriptCommon");Bundle scriptCommon = new Bundle("~/scripts/scriptCommon");

我在将 Bootstrap 从 4.5 版本升级到 5 时遇到了类似的问题。*

我在public static void RegisterBundles(BundleCollection bundles) { ... }方法中设置,来自:

bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include("~/Scripts/bootstrap.js"));
Run Code Online (Sandbox Code Playgroud)

到:

bundles.Add(new Bundle("~/bundles/bootstrap").Include("~/Scripts/bootstrap.js"));
Run Code Online (Sandbox Code Playgroud)

改为ScriptBundle刚刚Bundle

  • 为了澄清这个答案 - 为什么会出现这种情况:ScriptBundle 调用压缩器,如果存在问题(例如新的 js 规范,压缩器未经过测试来处理/脚本已经压缩),则 ScriptBundle 返回将为 null 。因此,要跳过捆绑中的缩小步骤,只需在添加到 BundleConfig.cs 条目时使用“Bundle”类型即可。对于动态包含的捆绑包尤其重要(因为这些捆绑包将在运行时失败并且难以测试)。可能有一个属性/过滤器来捕获这些异常并返回一个空包并记录错误。 (4认同)