ASP.NET MVC 3 - redirect to another action

C#asp.net MvcRedirect

C# Problem Overview


I want to redirect the Index action of the Home controller to another controller's action and nothing else. My code is thus:

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

Right now, this just loads a 0 kB blank page and does nothing. I have a feeling it has something to do with that void return type, but I don't know what else to change it to. What's the issue here?

C# Solutions


Solution 1 - C#

Your method needs to return a ActionResult type:

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

Solution 2 - C#

You will need to return the result of RedirectToAction.

Solution 3 - C#

Should Return ActionResult, instead of Void

Solution 4 - C#

You have to write this code instead of return View(); :

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

Solution 5 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenGCView Question on Stackoverflow
Solution 1 - C#The Scrum MeisterView Answer on Stackoverflow
Solution 2 - C#FemarefView Answer on Stackoverflow
Solution 3 - C#indiPyView Answer on Stackoverflow
Solution 4 - C#Soheil SoheiliView Answer on Stackoverflow
Solution 5 - C#Siby SunnyView Answer on Stackoverflow