Asp.net mvc 如何在GET上重定向到Action(),而不是在POST上
我有一个场景,我想在用户访问页面(GET,而不是POST)时重定向用户,我想知道如何在ASP.NETMVC中做到这一点 下面是一个场景。我有一个带有多步骤流程向导的控制器。尽管用户已经完成了步骤1,但仍有可能(即使不太可能)尝试访问该步骤。在这种情况下,我想让他转到第2步 比如: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) {
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();
}