Redirect to Action in another controller

315,180

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
)

MSDN

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 });
Share:
315,180
Sachin Kainth
Author by

Sachin Kainth

I am a senior C#, .NET and AngularJS developer living in London.

Updated on July 08, 2022

Comments

  • Sachin Kainth
    Sachin Kainth almost 2 years

    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?

  • Awais Mahmood
    Awais Mahmood over 8 years
    what 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
    Rory McCrossan over 8 years
    My second example, area = "", will do that for you.
  • Jonathan Alfaro
    Jonathan Alfaro over 7 years
    This 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
    user3071434 almost 5 years
    I 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
    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
    N_E over 2 years
    I 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
    noontz almost 2 years
    This seems to be sadly missing in .NET 6.0 or am I missing something?