问题描述
我有一个如下的异步控制器实现,
I have an Asynchronous controller implementation as follows,
public Task<ActionResult> UpdateUser(ProfileModel model)
{
return Task.Factory.StartNew(showMethod).ContinueWith(
t =>
{
return RedirectToAction("ViewUser","UserProfile");
});
}
但是,由于不断出现错误,我无法重定向到该操作,
However I am unable to redirect to the action as I am keep on getting the error,
无法隐式转换类型, System.Threading.Taska.Task<Sytem.Web.Mvc.RedirectToRouteResult>
为 System.Threading.Taska.Task<Sytem.Web.Mvc.ActionResult>
但是我真的想重定向到所提到的操作,我该怎么做.
However I really want to redirect to the mentioned Action, how can I do that.
推荐答案
您需要将UpdateUser
操作的返回类型从Task<ActionResult>
更改为Task<RedirectToRouteResult>
You need to change the return type of UpdateUser
action from Task<ActionResult>
to Task<RedirectToRouteResult>
public Task<RedirectToRouteResult> UpdateUser(ProfileModel model)
{
return Task.Factory.StartNew(showMethod).ContinueWith(
t => {
return RedirectToAction("ViewUser","UserProfile");
});
}
或者您可以使用ActionResult
显式设置ContinueWith
方法的泛型类型参数,如下所示:
Or you could explicitly set the generic type argument of ContinueWith
method with ActionResult
, like this:
public Task<ActionResult> UpdateUser(ProfileModel model)
{
return Task.Factory.StartNew(showMethod).ContinueWith<ActionResult>(
t => {
return RedirectToAction("ViewUser","UserProfile");
});
}
这篇关于如何在ASP.Net MVC中使用“任务"重定向到操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!