Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to Action in another controller

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?

like image 208
Sachin Kainth Avatar asked Oct 05 '22 18:10

Sachin Kainth


People also ask

How do I redirect another action to another controller?

Use this: return RedirectToAction("LogIn", "Account", new { area = "" }); This will redirect to the LogIn action in the Account controller in the "global" area.

Which return type is used to redirect to an action method of another controller?

An ActionResult is a return type of a controller method in MVC. Action methods help us to return models to views, file streams, and also redirect to another controller's Action method.

Which code statement is used to redirect to a controller action in a different area?

RedirectToAction(String, String) Redirects to the specified action using the action name and controller name.

How redirect a specific view from controller in MVC?

You can use the RedirectToAction() method, then the action you redirect to can return a View. The easiest way to do this is: return RedirectToAction("Index", model); Then in your Index method, return the view you want.


1 Answers

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.

like image 275
Rory McCrossan Avatar answered Oct 12 '22 23:10

Rory McCrossan