我正在使用 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.有没有更好的方法来做我想做的事?当您看到我返回 void 时,可能会出现一个危险信号,但这与正在使用 Edge.js 的事实有关。即便如此,我尝试在调用方法中返回 aTask
然后,但任务永远不会完成。task.Wait()