修复:'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>' ?
问题描述
如何解决以下错误? How to fix below error? 这篇关于修复:'await'运算符只能在异步方法中使用。考虑使用'async'修饰符标记此方法,并将其返回类型更改为'Task< System.Web.Mvc.ActionResult>' ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
'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);
}
返回查看();
}
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();
}