首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在GET而不是POST上使用RedirectToAction()

如何在GET而不是POST上使用RedirectToAction()
EN

Stack Overflow用户
提问于 2012-02-02 01:27:24
回答 2查看 5.4K关注 0票数 2

我有一个场景,我想在用户访问页面(GET,而不是POST)时重定向,我想知道如何在ASP.Net MVC中做到这一点。

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

类似于:

代码语言:javascript
复制
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”方法来做到这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-02 01:28:57

将返回类型更改为ActionResult,这是ViewResultRedirectToRouteResult的基类。

代码语言:javascript
复制
public ActionResult Step1(int? id)
{
    //Do some stuff and some checking here...
    if (step1done)
    {
        return RedirectToAction("RegisterStep2");
    }

    // ...

    return View();
}
票数 8
EN

Stack Overflow用户

发布于 2012-02-02 01:29:54

ViewResult更改为ActionResult

代码语言:javascript
复制
public ActionResult Step1(int? id)
{
    //Do some stuff and some checking here...
    if (step1done)
    {
        return RedirectToAction("RegisterStep2");
    }
}

ViewResult派生自abstractActionResult

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9100352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档