如何在ASP.Net MVC中使用“任务"重定向到操作 [英] How can I redirect to an action using Task in ASP.Net MVC

查看:75
本文介绍了如何在ASP.Net MVC中使用“任务"重定向到操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的异步控制器实现,

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中使用“任务"重定向到操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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