你如何在GET上RedirectToAction(),而不是在POST上

Jea*_*amp 2 asp.net-mvc redirect asp.net-mvc-3

我有一个场景,我想在访问页面时重定向用户(GET,而不是POST),我想知道如何在ASP.Net MVC中执行此操作.

这是场景.我有一个带有多步骤流程向导的控制器.即使不太可能,用户也可以尝试访问步骤1,尽管他已经完成了该步骤.在那种情况下,我想将他重定向到第2步.

就像是:

public ViewResult Step1(int? id)
{
    //Do some stuff and some checking here...
    if (step1done)
    {
        return RedirectToAction("RegisterStep2");
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,这会产生以下错误,因为RedirectToAction旨在用于ActionResult方法:

无法将类型'System.Web.Mvc.RedirectToRouteResult'隐式转换为'System.Web.Mvc.ViewResult'

有人能告诉我如何解决这个问题并让我的ViewResult方法(GET操作)执行重定向吗?我应该像普通的旧ASP.Net一样简单地使用Response.Redirect(),还是有"更多的ASP.Net MVC"方式来做到这一点?

jru*_*ell 8

改变你的返回类型ActionResult,两者的基类ViewResultRedirectToRouteResult.

public ActionResult Step1(int? id)
{
    //Do some stuff and some checking here...
    if (step1done)
    {
        return RedirectToAction("RegisterStep2");
    }

    // ...

    return View();
}
Run Code Online (Sandbox Code Playgroud)


gdo*_*ica 6

更改ViewResultActionResult

public ActionResult Step1(int? id)
{
    //Do some stuff and some checking here...
    if (step1done)
    {
        return RedirectToAction("RegisterStep2");
    }
}
Run Code Online (Sandbox Code Playgroud)

ViewResult来自abstract班级ActionResult.