TBA TBA - 3 months ago 50
ASP.NET (C#) Question

How can I redirect to an action using Task in 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,

Cannot implicitly convert type,
System.Threading.Taska.Task<Sytem.Web.Mvc.RedirectToRouteResult>
to
System.Threading.Taska.Task<Sytem.Web.Mvc.ActionResult>


However I really want to redirect to the mentioned Action, how can I do that.

Answer

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");
  });
}

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");
  });
}