Redirect to Action in another controller
Solution 1
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
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
)
Solution 3
You can use this:
return RedirectToAction("actionName", "controllerName", new { area = "Admin" });
Solution 4
Use this:
return this.RedirectToAction<AccountController>(m => m.LogIn());
Solution 5
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 });
Sachin Kainth
I am a senior C#, .NET and AngularJS developer living in London.
Updated on July 08, 2022Comments
-
Sachin Kainth almost 2 years
I have two controllers, both called
AccountController
. One of them, lets call itController A
, is in anArea
calledAdmin
and the other, lets call itController B
, is not in anyArea
(I guess that means it's in the defaultArea
?).Controller B
has anaction method
calledLogin
. I have anaction method
inController A
, which has this linereturn 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-existentaction
inController A
. I want to call theaction method
inController B
. Is this possible? -
Awais Mahmood over 8 yearswhat if I want to go from a view in a certain area to action of a controller which is not in any area. Like in MVC5, the LogOff button on top right is in AccountController, which donot reside in any area. And I want to LogOff from a view in a certain area???
-
Rory McCrossan over 8 yearsMy second example,
area = ""
, will do that for you. -
Jonathan Alfaro over 7 yearsThis worked for me with ASP.NET Core.... When I upgraded to the latest version the RedirectToAction broke and I got it to work using the area = "" with an empty string.
-
user3071434 almost 5 yearsI like the concept. I have always hated the string part of RedirectToAction and thought it should be more like what you entered, but this appears to anger c#. Is this in frameworks newer then 4.6.2?
-
Hiren Patel almost 5 years@user3071434 No, you can use with adding "using Microsoft.Web.Mvc". you can avoid string part and reduce to getting an error on runtime due to the wrong Action text
-
N_E over 2 yearsI notice a caveat using this approach is that you would creating a new request that will pipe down through Routing engine and then reach the controller. If you are making several indirections, response may come out slower compared to RedirectToAction
-
noontz almost 2 yearsThis seems to be sadly missing in .NET 6.0 or am I missing something?