繁体   English   中英

对于Visual Studio MVC项目,将页面标记为“异步= true”

[英]Mark page as “async = true” for a Visual Studio MVC Project

我正在使用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() ,但任务从未完成。

在尝试了一些不同的操作之后,以下解决方案为我工作。

尽管我回答了自己的问题,而且看起来很琐碎,但我并未删除此问题,因为Web上没有太多有关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);
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM