ASP.NET MVC 3-重定向到另一个操作


70

我想将Home控制器的Index操作重定向到另一个控制器的操作,仅此而已。因此,我的代码是:

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

现在,这只会加载一个0 kB的空白页面,什么也不做。我感觉它与该void返回类型有关,但是我不知道将其更改为什么。这是什么问题?

Answers:


148

您的方法需要返回一个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”});
狮子座



11

您必须编写此代码,而不是返回View();。:

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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.