修复:'await'运算符只能在异步方法中使用。考虑使用'async'修饰符标记此方法,并将其返回类型更改为'Task< System.Web.Mvc.ActionResult>' ? [英] Fix for : The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task<System.Web.Mvc.ActionResult>' ?

查看:842
本文介绍了修复:'await'运算符只能在异步方法中使用。考虑使用'async'修饰符标记此方法,并将其返回类型更改为'Task< System.Web.Mvc.ActionResult>' ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决以下错误?

'await'运算符只能在异步方法中使用。考虑使用'async'修饰符标记此方法,并将其返回类型更改为'Task< system.web.mvc.actionresult>'



上述错误。我真的对此一无所知。有谁可以帮我这个?

我的代码,

<前lang =c#> [HttpPost]
public ActionResult Createaccount(Register reg)
{
if (ModelState.IsValid)
{
var client = new MongoClient();
var db = client.GetDatabase( foo的);

var collection = db.GetCollection< Register>( );
var user = new 注册{fname = reg.fname,lname = reg.lname, age = reg.age,pwd = reg.pwd,email = reg.email};

await collection.InsertOneAsync(user);
TempData [ msg] = sucess;
ModelState.Clear();
return 重定向( / angular / createaccount );

}

return View();
}





如果有人有任何想法请分享。

解决方案

您需要在方法签名中包含async标识符。 MSDN是你的朋友:

https://msdn.microsoft.com/en -us / library / hh191443.aspx [ ^ ]


//如果.net框架为4或4.5或更高版本,它将起作用



[HttpPost]

公共异步任务< ActionResult> Createaccount(注册注册)

{

if(ModelState.IsValid)

{

var client = new MongoClient ();

var db = client.GetDatabase(foo);

var collection = db.GetCollection< register>(people);

var user = new Register {fname = reg.fname,lname = reg.lname,age = reg.age,pwd = reg.pwd,email = reg.email};



await collection.InsertOneAsync(user);

TempData [msg] =sucess;

ModelState.Clear();

返回Redirect(/ angular / createaccount);



}



返回查看();

}


How to fix below error?
The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task<system.web.mvc.actionresult>'

Am getting above error. I really don't have any idea about this. Could anyone help me on this?
My code,

[HttpPost]
     public ActionResult Createaccount(Register reg)
     {
         if (ModelState.IsValid)
         {
             var client = new MongoClient();
             var db = client.GetDatabase("foo");

             var collection = db.GetCollection<Register>("people");
             var user = new Register { fname = reg.fname, lname = reg.lname, age = reg.age, pwd = reg.pwd, email = reg.email };

             await collection.InsertOneAsync(user);
             TempData["msg"] = "sucess";
             ModelState.Clear();
             return Redirect("/angular/createaccount");

         }

         return View();
     }



If anybody has any idea please share it.

解决方案

You Need to include "async" identifier in your method signature. MSDN is your friend:
https://msdn.microsoft.com/en-us/library/hh191443.aspx[^]


// it will work if your .net framework would be 4 or 4.5 or upper

[HttpPost]
public async Task<ActionResult> Createaccount(Register reg)
{
if (ModelState.IsValid)
{
var client = new MongoClient();
var db = client.GetDatabase("foo");
var collection = db.GetCollection<register>("people");
var user = new Register { fname = reg.fname, lname = reg.lname, age = reg.age, pwd = reg.pwd, email = reg.email };

await collection.InsertOneAsync(user);
TempData["msg"] = "sucess";
ModelState.Clear();
return Redirect("/angular/createaccount");

}

return View();
}


这篇关于修复:'await'运算符只能在异步方法中使用。考虑使用'async'修饰符标记此方法,并将其返回类型更改为'Task&lt; System.Web.Mvc.ActionResult&gt;' ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆