我有一个场景,我想在用户访问页面(GET,而不是POST)时重定向,我想知道如何在ASP.Net MVC中做到这一点。
这是一个场景。我有一个带有多步骤流程向导的控制器。即使不太可能,用户也可能尝试访问步骤1,尽管他已经完成了该步骤。在这种情况下,我想将他重定向到步骤2。
类似于:
public ViewResult Step1(int? id)
{
//Do some stuff and some checking here...
if (step1done)
{
return RedirectToAction("RegisterStep2");
}
}但是,这会产生以下错误,因为RedirectToAction应在ActionResult方法中使用:
不能将类型'System.Web.Mvc.RedirectToRouteResult‘隐式转换为'System.Web.Mvc.ViewResult’
有人能告诉我如何修复这个问题并让我的ViewResult方法(GET操作)执行重定向吗?我应该像在普通的老式ASP.Net中一样简单地使用Response.Redirect(),还是有一种“更多的ASP.Net MVC”方法来做到这一点?
发布于 2012-02-02 01:28:57
将返回类型更改为ActionResult,这是ViewResult和RedirectToRouteResult的基类。
public ActionResult Step1(int? id)
{
//Do some stuff and some checking here...
if (step1done)
{
return RedirectToAction("RegisterStep2");
}
// ...
return View();
}发布于 2012-02-02 01:29:54
将ViewResult更改为ActionResult
public ActionResult Step1(int? id)
{
//Do some stuff and some checking here...
if (step1done)
{
return RedirectToAction("RegisterStep2");
}
}ViewResult派生自abstract类ActionResult。
https://stackoverflow.com/questions/9100352
复制相似问题