将页面标记为“async = true”,用于Visual Studio MVC项目

我正在使用Edge.js,以便我可以从C#中调用Node.js。 根据链接中的文档,我会做类似于以下内容:

[HttpPost] public ActionResult Input(InputModel obj) { validateInput(obj); return View(); } private async void validateInput(object obj) { var func = Edge.Func(@" return function (data, callback){ var username = data.username, email = data.email; callback(null, username); } "); ViewBag.Msg = (string)await func(obj); } 

但是,我得到以下运行时错误:

  Additional information: An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it. 

我的问题是双重的:

1.如何制作页面,async = true。 我知道如何做到这一点的Web表单项目,而不是一个MVC项目。

2.有没有更好的方法去做我想做的事情? 当你看到我正在返回无效的时候,红旗可能会上升,但这是对Edge.js正在使用的事实。 即使如此,我已经尝试返回一个Task ,然后task.Wait()在调用方法,但任务永远不会结束。

尝试了一些不同的东西后,下面的解决scheme为我工作。

尽pipe我回答了我自己的问题,而且看起来微不足道,但我并没有去除这个问题,因为网上关于Edge.js的知识并不多。

  [HttpPost] public async Task<ActionResult> Input(InputModel obj) { ViewBag.Msg = await validateInput(obj); return View(); } private async Task<string> validateInput(object obj) { var func = Edge.Func(@" return function (data, callback){ var username = data.username, email = data.email; callback(null, username); } "); return (string)await func(obj); }