I have a scenario where I would like to redirect the user while he is accessing a page (GET, not POST), and I would like to know how to do this in ASP.Net MVC.
Here is the scenario. I have a controller with a multi-step process wizard. It is possible, even if unlikely, that a user tries to access step 1 although he has already completed that step. In that case, I would like to redirect him to step 2.
Something like:
public ViewResult Step1(int? id)
{
//Do some stuff and some checking here...
if (step1done)
{
return RedirectToAction("RegisterStep2");
}
}
However, this gives the following error, because RedirectToAction is meant to be used in the ActionResult method:
Cannot implicitly convert type ‘System.Web.Mvc.RedirectToRouteResult’ to ‘System.Web.Mvc.ViewResult’
Can someone tell me how to fix this and have my ViewResult method (GET action) perform a redirection? Should I simply use Response.Redirect() just as in plain old ASP.Net, or is there a “more ASP.Net MVC” way to do this?
Change your return type to
ActionResult, the base class for bothViewResultandRedirectToRouteResult.