166

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?

1

8 Answers 8

294

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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???
My second example, area = "", will do that for you.
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.
33

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

Comments

10

You can use this:

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

Comments

3

Use this:

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

3 Comments

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?
@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
This seems to be sadly missing in .NET 6.0 or am I missing something?
3

This should work

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

Comments

1

Try switching them:

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

I tried it and it worked.

Comments

0

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 });

1 Comment

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
-1

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

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.