微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我的 js 文件中有异步方法时,ASP.NET MVC 绑定失败

如何解决当我的 js 文件中有异步方法时,ASP.NET MVC 绑定失败

我们开始移动遗留 js 脚本以符合最新规范,并将一些 js 方法更改为异步。例如

const persistMode = async (serverUrl,id,mode) => {
   const data = JSON.stringify({id: id,mode: mode});
   const ajaxProperties = {
       type: "POST",url: serverUrl,data: data,dataType: "json",contentType: "application/json"
   };
   return await $.ajax(ajaxProperties);
};

现在我定义了一个包并在 cshtml 文件中引用它,如下所示

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

现在,当视图加载时,我收到以下错误

    System.NullReferenceException: Object reference not set to an instance of an object.
   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)
   at Microsoft.Ajax.Utilities.JSParser.ParseExpressionStatement(Boolean fSourceElement)
   at Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean fSourceElement,Boolean skipImportantComment)
   at Microsoft.Ajax.Utilities.JSParser.ParseStatements(Block block)
   at Microsoft.Ajax.Utilities.JSParser.InternalParse()
   at Microsoft.Ajax.Utilities.Minifier.MinifyJavaScript(String source,CodeSettings codeSettings)
   at System.Web.Optimization.JsMinify.Process(BundleContext context,BundleResponse response)
   at System.Web.Optimization.Bundle.ApplyTransforms(BundleContext context,String bundleContent,IEnumerable`1 bundleFiles)
   at System.Web.Optimization.Bundle.GetBundleResponse(BundleContext context)
   at System.Web.Optimization.BundleResolver.GetBundleContents(String virtualPath)
   at System.Web.Optimization.AssetManager.DeterminePathsToRender(IEnumerable`1 assets)
   at System.Web.Optimization.AssetManager.RenderExplicit(String tagFormat,String[] paths)
   at ASP._Page_Views_xxx__xxxxxxxx_cshtml.Execute() in xxxxxxxxxxxxxxxxxxx:line 479
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.StartPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext,TextWriter writer,WebPageRenderingBase startPage)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters,Int32 filterIndex,ResultExecutingContext preContext,ControllerContext controllerContext,ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters,ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext,IList`1 filters,ActionResult actionResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__displayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult);

知道这里出了什么问题。而且,如果我从所述方法删除 async/await,则捆绑工作。

解决方法

尝试使用这个算法:

libmagic

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。