Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何在GET上重定向到Action(),而不是在POST上_Asp.net Mvc_Asp.net Mvc 3_Redirect - Fatal编程技术网

Asp.net mvc 如何在GET上重定向到Action(),而不是在POST上

Asp.net mvc 如何在GET上重定向到Action(),而不是在POST上,asp.net-mvc,asp.net-mvc-3,redirect,Asp.net Mvc,Asp.net Mvc 3,Redirect,我有一个场景,我想在用户访问页面(GET,而不是POST)时重定向用户,我想知道如何在ASP.NETMVC中做到这一点 下面是一个场景。我有一个带有多步骤流程向导的控制器。尽管用户已经完成了步骤1,但仍有可能(即使不太可能)尝试访问该步骤。在这种情况下,我想让他转到第2步 比如: public ViewResult Step1(int? id) { //Do some stuff and some checking here... if (step1done) {

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

下面是一个场景。我有一个带有多步骤流程向导的控制器。尽管用户已经完成了步骤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 action)执行重定向吗?我应该像在普通的旧ASP.Net中一样简单地使用Response.Redirect(),还是有一种“更多的ASP.Net MVC”方法来实现这一点?

将您的返回类型更改为,ViewResult和
RedirectToRouteResult
的基类

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

    // ...

    return View();
}

ViewResult
更改为
ActionResult

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

ViewResult
派生自
abstract
ActionResult

只需将返回类型更改为ActionResult,因为您并不总是返回视图。在if子句之后还必须有一个返回语句。@Tomasjanson是的,谢谢。这只是一段过于简化的代码,只是为了说明我在做什么。我不知道GET操作可以返回ActionResult。我以为这只是为了事后行动。我试试看。
public ActionResult Step1(int? id)
{
    //Do some stuff and some checking here...
    if (step1done)
    {
        return RedirectToAction("RegisterStep2");
    }

    // ...

    return View();
}