我正在使用 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。即便如此,我尝试在调用方法中返回一个 Task
,然后返回 task.Wait()
但任务永远不会完成。
最佳答案
尝试了一些不同的方法后,以下解决方案对我有用。
尽管我回答了我自己的问题,而且这看起来微不足道,但我不会删除这个问题,因为网上关于 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);
}
关于asp.net-mvc - 将 Visual Studio MVC 项目的页面标记为 "async = true",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34231926/