gpt4 book ai didi

asp.net-mvc - 你如何在 GET 而不是 POST 上使用 RedirectToAction()

转载 作者:行者123 更新时间:2023-12-02 05:38:29 24 4
gpt4 key购买 nike

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

这是场景。我有一个带有多步过程向导的 Controller 。即使不太可能,用户也有可能尝试访问步骤 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”的方式来做到这一点?

最佳答案

将返回类型更改为 ActionResultViewResultRedirectToRouteResult 的基类。

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

// ...

return View();
}

关于asp.net-mvc - 你如何在 GET 而不是 POST 上使用 RedirectToAction(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9100352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com