How do I redirect from one controller to another?

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

  1. what if I want to go from a view in a certain area to action of a controller which is not in any area.
  2. My second example, area = “” , will do that for you.

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.

How can we call one controller method to another controller in MVC?

We can do it by some of the following ways:

  1. By directly redirecting- return RedirectToAction(“MethodName”, “ControllerName”);
  2. By creating object – ControllerName objController=new ControllerName(); objController. methodName(parameters)

How pass value from one controller to another in MVC 5?

ViewData, ViewBag, and TempData are used to pass data between controller, action, and views. To pass data from the controller to view, either ViewData or ViewBag can be used. To pass data from one controller to another controller, TempData can be used.

Can a controller call another controller?

Yes, you can call a method of another controller. The controller is also a simple class.

How can I call API from one controller to another controller?

To be able to use a controller from another controller you need to:

  1. Register the controller in Startup. cs ConfigureServices: services. AddTransient
  2. You must pass the controller you want to access as a ctor parameter into the main controller.

What is difference between TempData and ViewData?

ViewData is a dictionary object while ViewBag is a dynamic property (a new C# 4.0 feature). TempData is also a dictionary object that stays for the time of an HTTP Request. So, Tempdata can be used to maintain data between redirects i.e from one controller to the other controller.

How do you call an action from one controller to another controller?

var ctrl= new MyController(); ctrl. ControllerContext = ControllerContext; //call action return ctrl. Action();