C# 如何从c中的异步操作方法中获取值#
我有一个C# 如何从c中的异步操作方法中获取值#,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我有一个async方法,比如 [HttpPost] public async Task<JsonResult> AddTwoIntegers(int param1, int param2) { var result = await (param1 + param2); return Json(new {finalValue: result}, JsonRequestBehavior.AllowGet) } Type dat
async
方法,比如
[HttpPost]
public async Task<JsonResult> AddTwoIntegers(int param1, int param2)
{
var result = await (param1 + param2);
return Json(new {finalValue: result}, JsonRequestBehavior.AllowGet)
}
Type data = null; //Note that I put Type because I don't know your type for result.Data
jsonData.ContinueWith(task =>
{
JsonResult result = task.Result;
if (result.Data.ToString() == "") {
data = result.Data;
}
});
// I want to retrieve the value returned and use that value in some operation.
//Now you can use your variable data here!!
return Json("Success", JsonRequestBehavior.AllowGet);
如何从
操作结果
获取返回值 应该是这样的:
JsonResult = await AddTwoIntegers(5,10);
Type data = null; //Note that I put Type because I don't know your type for result.Data
jsonData.ContinueWith(task =>
{
JsonResult result = task.Result;
if (result.Data.ToString() == "") {
data = result.Data;
}
});
// I want to retrieve the value returned and use that value in some operation.
//Now you can use your variable data here!!
return Json("Success", JsonRequestBehavior.AllowGet);
检查它应该是这样的:
JsonResult = await AddTwoIntegers(5,10);
Type data = null; //Note that I put Type because I don't know your type for result.Data
jsonData.ContinueWith(task =>
{
JsonResult result = task.Result;
if (result.Data.ToString() == "") {
data = result.Data;
}
});
// I want to retrieve the value returned and use that value in some operation.
//Now you can use your variable data here!!
return Json("Success", JsonRequestBehavior.AllowGet);
选中则需要声明在操作外部存储结果的变量。然后使用变量
Type data = null; //Note that I put Type because I don't know your type for result.Data
jsonData.ContinueWith(task =>
{
JsonResult result = task.Result;
if (result.Data.ToString() == "") {
data = result.Data;
}
});
// I want to retrieve the value returned and use that value in some operation.
//Now you can use your variable data here!!
return Json("Success", JsonRequestBehavior.AllowGet);
Task jsonData=this.addTwoInteger(5,10)
Type data = null; //Note that I put Type because I don't know your type for result.Data
jsonData.ContinueWith(task =>
{
JsonResult result = task.Result;
if (result.Data.ToString() == "") {
data = result.Data;
}
});
// I want to retrieve the value returned and use that value in some operation.
//Now you can use your variable data here!!
return Json("Success", JsonRequestBehavior.AllowGet);
}然后需要声明在操作外部存储结果的变量。然后使用变量
Type data = null; //Note that I put Type because I don't know your type for result.Data
jsonData.ContinueWith(task =>
{
JsonResult result = task.Result;
if (result.Data.ToString() == "") {
data = result.Data;
}
});
// I want to retrieve the value returned and use that value in some operation.
//Now you can use your variable data here!!
return Json("Success", JsonRequestBehavior.AllowGet);
Task jsonData=this.addTwoInteger(5,10)
Type data = null; //Note that I put Type because I don't know your type for result.Data
jsonData.ContinueWith(task =>
{
JsonResult result = task.Result;
if (result.Data.ToString() == "") {
data = result.Data;
}
});
// I want to retrieve the value returned and use that value in some operation.
//Now you can use your variable data here!!
return Json("Success", JsonRequestBehavior.AllowGet);
}您需要更改返回
任务的方法,并将其标记为async
,然后等待AddTwoIntegers()
的结果
Type data = null; //Note that I put Type because I don't know your type for result.Data
jsonData.ContinueWith(task =>
{
JsonResult result = task.Result;
if (result.Data.ToString() == "") {
data = result.Data;
}
});
// I want to retrieve the value returned and use that value in some operation.
//Now you can use your variable data here!!
return Json("Success", JsonRequestBehavior.AllowGet);
public async Task<ActionResult> SomeFunction(string userSettingsViewModel)
{
JsonResult result = await this.AddTwoIntegers(5, 10);
var jsonData = result.data;
// ...
return Json("Success", JsonRequestBehavior.AllowGet);
}
public async Task SomeFunction(字符串usersetingsviewmodel)
{
JsonResult=wait this.addTwoInteger(5,10);
var jsonData=result.data;
// ...
返回Json(“Success”,JsonRequestBehavior.AllowGet);
}
为了完整性,示例方法应该如下所示
Type data = null; //Note that I put Type because I don't know your type for result.Data
jsonData.ContinueWith(task =>
{
JsonResult result = task.Result;
if (result.Data.ToString() == "") {
data = result.Data;
}
});
// I want to retrieve the value returned and use that value in some operation.
//Now you can use your variable data here!!
return Json("Success", JsonRequestBehavior.AllowGet);
[HttpPost]
public Task<JsonResult> AddTwoIntegers(int param1, int param2)
{
var result = param1 + param2;
return Task.FromResult(Json(new {finalValue: result},
JsonRequestBehavior.AllowGet));
}
[HttpPost]
公共任务addTwoInteger(int-param1,int-param2)
{
var结果=参数1+参数2;
返回Task.FromResult(Json)(新的{finalValue:result},
JsonRequestBehavior.AllowGet));
}
您需要更改返回任务的方法,并将其标记为async
,然后等待AddTwoIntegers()
的结果
Type data = null; //Note that I put Type because I don't know your type for result.Data
jsonData.ContinueWith(task =>
{
JsonResult result = task.Result;
if (result.Data.ToString() == "") {
data = result.Data;
}
});
// I want to retrieve the value returned and use that value in some operation.
//Now you can use your variable data here!!
return Json("Success", JsonRequestBehavior.AllowGet);
public async Task<ActionResult> SomeFunction(string userSettingsViewModel)
{
JsonResult result = await this.AddTwoIntegers(5, 10);
var jsonData = result.data;
// ...
return Json("Success", JsonRequestBehavior.AllowGet);
}
public async Task SomeFunction(字符串usersetingsviewmodel)
{
JsonResult=wait this.addTwoInteger(5,10);
var jsonData=result.data;
// ...
返回Json(“Success”,JsonRequestBehavior.AllowGet);
}
为了完整性,示例方法应该如下所示
Type data = null; //Note that I put Type because I don't know your type for result.Data
jsonData.ContinueWith(task =>
{
JsonResult result = task.Result;
if (result.Data.ToString() == "") {
data = result.Data;
}
});
// I want to retrieve the value returned and use that value in some operation.
//Now you can use your variable data here!!
return Json("Success", JsonRequestBehavior.AllowGet);
[HttpPost]
public Task<JsonResult> AddTwoIntegers(int param1, int param2)
{
var result = param1 + param2;
return Task.FromResult(Json(new {finalValue: result},
JsonRequestBehavior.AllowGet));
}
[HttpPost]
公共任务addTwoInteger(int-param1,int-param2)
{
var结果=参数1+参数2;
返回Task.FromResult(Json)(新的{finalValue:result},
JsonRequestBehavior.AllowGet));
}
等待(param1+param2)代码>酷!这甚至可以编译吗?您不能等待int
<代码>等待(param1+param2)
我的用例与我给出的示例代码不同。我实际需要的是,我希望在另一个操作方法中访问该值。但它只是从那里跳出来的。我没有得到应有的价值,最好展示一下你所做的。因为使用async
和await
编写的代码是可以避免的。你不能那样使用它。@lnanikian-yah这一切都是关于更改密码之类的。我不能在这里写它。wait(param1+param2)代码>酷!这甚至可以编译吗?您不能等待int
<代码>等待(param1+param2)
我的用例与我给出的示例代码不同。我实际需要的是,我希望在另一个操作方法中访问该值。但它只是从那里跳出来的。我没有得到应有的价值,最好展示一下你所做的。因为使用async
和await
编写的代码是可以避免的。你不能那样使用它。@lnanikian-yah这一切都是关于更改密码之类的。我不能写在这里,我的问题是我能把这两个值相加吗。当我返回json Result时。我的问题是,我能把这两个值相加吗。当我返回json Result时。如何访问返回整数值的函数