2011-02-05 100 views
55

我想將主控制器的Index操作重定向到另一個控制器的操作,而不是其他任何操作。我的代碼是這樣的:ASP.NET MVC 3 - 重定向到另一個操作

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

現在,這只是加載一個0 kB空白頁並且什麼都不做。我有一種感覺,它與無效返回類型有關,但我不知道還有什麼可以改變它的。這裏有什麼問題?

回答

125

你的方法需要返回一個ActionResult類型:

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

您將需要返回RedirectToAction的結果。

5

你必須寫這個代碼,而不是返回查看(); :

return RedirectToAction("ActionName", "ControllerName"); 
-1
return RedirectToAction("ActionName", "ControllerName"); 
+0

會幫助,如果你提供一些背景和解釋,爲什麼這會工作。 – jwenting 2017-03-20 09:52:03

相關問題