Redirect to Action in another controller

asp.net Mvcasp.net Mvc-3C# 4.0Redirecttoaction

asp.net Mvc Problem Overview


I have two controllers, both called AccountController. One of them, lets call it Controller A, is in an Area called Admin and the other, lets call it Controller B, is not in any Area (I guess that means it's in the default Area?). Controller B has an action method called Login. I have an action method in Controller A, which has this line

return RedirectToAction("LogIn", "Account");

The problem is that I get a 404 when this line gets executed because an attempt is made to redirect to a non-existent action in Controller A. I want to call the action method in Controller B. Is this possible?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

You can supply the area in the routeValues parameter. Try this:

return RedirectToAction("LogIn", "Account", new { area = "Admin" });

Or

return RedirectToAction("LogIn", "Account", new { area = "" });

depending on which area you're aiming for.

Solution 2 - asp.net Mvc

Use this:

return RedirectToAction("LogIn", "Account", new { area = "" });

This will redirect to the LogIn action in the Account controller in the "global" area.

It's using this RedirectToAction overload:

protected internal RedirectToRouteResult RedirectToAction(
	string actionName,
	string controllerName,
	Object routeValues
)

MSDN

Solution 3 - asp.net Mvc

You can use this:

return RedirectToAction("actionName", "controllerName", new { area = "Admin" });

Solution 4 - asp.net Mvc

Use this:

    return this.RedirectToAction<AccountController>(m => m.LogIn());

Solution 5 - asp.net Mvc

RedirectToRoute() is also available. Also, a better way to do it might be using nameof() so you can avoid hardcoding strings in your codebase.

return RedirectToRoute(nameof(AccountController) + nameof(AccountController.Login));

And, if you are redirecting to an endpoint that takes parameters, pass those along.

return RedirectToRoute(nameof(Account) + nameof(Account.ChangePassword), new { id = id });

Solution 6 - asp.net Mvc

Try switching them:

return RedirectToAction("Account", "Login");

I tried it and it worked.

Solution 7 - asp.net Mvc

This should work

return RedirectToAction("actionName", "controllerName", null);

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
QuestionSachin KainthView Question on Stackoverflow
Solution 1 - asp.net MvcRory McCrossanView Answer on Stackoverflow
Solution 2 - asp.net Mvcgdoron is supporting MonicaView Answer on Stackoverflow
Solution 3 - asp.net MvcMuhammad AwaisView Answer on Stackoverflow
Solution 4 - asp.net MvcHiren PatelView Answer on Stackoverflow
Solution 5 - asp.net MvcLinjuView Answer on Stackoverflow
Solution 6 - asp.net MvcRaymond IraniView Answer on Stackoverflow
Solution 7 - asp.net MvcKazeem QuadriView Answer on Stackoverflow