How to redirect to Index from another controller?

C#asp.net Mvc

C# Problem Overview


I have been looking through trying to find some way to redirect to an Index view from another controller.

public ActionResult Index()
{                
     ApplicationController viewModel = new ApplicationController();
     return RedirectToAction("Index", viewModel);
}

This is what I tried right now. Now the code I was given to has a ActionLink that links to the page I need to Redirect too.

@Html.ActionLink("Bally Applications","../Application")

C# Solutions


Solution 1 - C#

Use the overloads that take the controller name too...

return RedirectToAction("Index", "MyController");

and

@Html.ActionLink("Link Name","Index", "MyController", null, null)

Solution 2 - C#

try:

public ActionResult Index() {
    return RedirectToAction("actionName");
    // or
    return RedirectToAction("actionName", "controllerName");
    // or
    return RedirectToAction("actionName", "controllerName", new {/* routeValues, for example: */ id = 5 });
}

and in .cshtml view:

@Html.ActionLink("linkText","actionName")

OR:

@Html.ActionLink("linkText","actionName","controllerName")

OR:

@Html.ActionLink("linkText", "actionName", "controllerName", 
    new { /* routeValues forexample: id = 6 or leave blank or use null */ }, 
    new { /* htmlAttributes forexample: @class = "my-class" or leave blank or use null */ })

Notice using null in final expression is not recommended, and is better to use a blank new {} instead of null

Solution 3 - C#

You can use the following code:

return RedirectToAction("Index", "Home");

See RedirectToAction

Solution 4 - C#

You can use the overloads method RedirectToAction(string actionName, string controllerName);

Example:

RedirectToAction(nameof(HomeController.Index), "Home");
 

Solution 5 - C#

You can use local redirect. Following codes are jumping the HomeController's Index page:

public class SharedController : Controller
    {
        // GET: /<controller>/
        public IActionResult _Layout(string btnLogout)
        {
            if (btnLogout != null)
            {
                return LocalRedirect("~/Index");
            }

            return View();
        }
}

Solution 6 - C#

Complete answer (.Net Core 3.1)

Most answers here are correct but taken a bit out of context, so I will provide a full-fledged answer which works for Asp.Net Core 3.1. For completeness' sake:

[Route("health")]
[ApiController]
public class HealthController : Controller
{
    [HttpGet("some_health_url")]
    public ActionResult SomeHealthMethod() {}
}

[Route("v2")]
[ApiController]
public class V2Controller : Controller
{
    [HttpGet("some_url")]
    public ActionResult SomeV2Method()
    {
        return RedirectToAction("SomeHealthMethod", "Health"); // omit "Controller"
    }
}

If you try to use any of the url-specific strings, e.g. "some_health_url", it will not work!

Solution 7 - C#

Tag helpers:

<a asp-controller="OtherController" asp-action="Index" class="btn btn-primary"> Back to Other Controller View </a>

In the controller.cs have a method:

public async Task<IActionResult> Index()
{
    ViewBag.Title = "Titles";
    return View(await Your_Model or Service method);
}

Solution 8 - C#

RedirectToRoute() is another option. Just pass the route as the argument. Also, using nameof() might be a better convention since you are not hard coding the controller name as a string.

 return RedirectToRoute(nameof(HomeController) + nameof(HomeController.Index));

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
Questioncjohnson2136View Question on Stackoverflow
Solution 1 - C#musefanView Answer on Stackoverflow
Solution 2 - C#amiry jdView Answer on Stackoverflow
Solution 3 - C#Wouter de KortView Answer on Stackoverflow
Solution 4 - C#A. LipnitskiyView Answer on Stackoverflow
Solution 5 - C#Hasan Tuna OruçView Answer on Stackoverflow
Solution 6 - C#alexpanterView Answer on Stackoverflow
Solution 7 - C#Hanna FroebeView Answer on Stackoverflow
Solution 8 - C#LinjuView Answer on Stackoverflow