【问题标题】:ASP.NET MVC 3 - redirect to another actionASP.NET MVC 3 - 重定向到另一个动作
【发布时间】:2011-06-22 00:48:45
【问题描述】:

我想将 Home 控制器的 Index 操作重定向到另一个控制器的操作,而不是别的。我的代码是这样的:

    public void Index()
    {
        //All we want to do is redirect to the class selection page
        RedirectToAction("SelectClasses", "Registration");
    }

现在,这只是加载一个 0 kB 的空白页,什么都不做。我觉得它与那个 void 返回类型有关,但我不知道还有什么可以改变的。这里有什么问题?

【问题讨论】:

    标签: c# asp.net-mvc redirect


    【解决方案1】:
    return RedirectToAction("ActionName", "ControllerName");
    

    【讨论】:

    • 如果您提供一些上下文和解释说明为什么这会起作用,将会有所帮助。
    【解决方案2】:

    您必须编写此代码而不是 return View(); :

    return RedirectToAction("ActionName", "ControllerName");
    

    【讨论】:

      【解决方案3】:

      你的方法需要返回一个ActionResult 类型:

      public ActionResult Index()
      {
          //All we want to do is redirect to the class selection page
          return RedirectToAction("SelectClasses", "Registration");
      }
      

      【讨论】:

      • 如果你需要传递一些参数:return RedirectToAction("ActionName", "ControllerName", new { id = 99, name="sample" });
      【解决方案4】:

      应该ReturnActionResult,而不是Void

      【讨论】:

        【解决方案5】:

        你需要返回RedirectToAction的结果。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-31
          • 1970-01-01
          • 1970-01-01
          • 2019-11-14
          相关资源
          最近更新 更多