In this article, we will learn how to call an action from another controller in .NET MVC.

To download the source code for this article, you can visit our GitHub repository.

We sometimes need to call an action method from another action method within the same controller or a different controller class. 

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!

So, let’s see how to do that.

Why We Need to Call the Action From Another Controller?

Before we move ahead, let’s first understand why we need to call the action from another controller in .NET MVC. Usually, we don’t do this because the role of the controller is not to deal with business logic.

However, sometimes a situation arises where we need to call the action from another controller, and at that time it becomes beneficial.

Some of the functionalities work across the application such as authentication, error logging, etc. We can call the action from another controller to ensure consistency and avoid repetitive code. 

Also, for example inside the online shop app. We may want to add a product to the cart visible on the Home page of an application.

In that case, when a user clicks the Add To Cart button, the related action from the Home controller can check if the product is available and if it is, the user will be redirected to the Cart controller to see the product inside the cart.

So, this is just one scenario where we need to call an action method from another controller.

Let’s understand the different ways to do this in .NET MVC.

Calling a Controller Action from Another Controller

Routing in MVC means moving from one action method to another, and the action methods can be in the same or different controller.

.NET MVC provides multiple ways of calling an action method from another controller class. So, let’s take a look at some of them to achieve this.

If you want to know more about routing in .NET Core MVC, check out our article Routing in ASP.NET Core MVC.

Use the RedirectToAction() Method

If we wish to redirect or call a controller action method from another controller class then we can use the RedirectToAction() method. This method redirects the request to the specific action method.

The method accepts the action name as the first argument and the name of the controller class as the second argument:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return RedirectToAction("List", "Product");
    }
}

Here, we pass 2 arguments to the RedirectToAction() method. In this case, List is the name of the action method and Product is the name of the controller class.

So, in this way, the Index() method of HomeController will call the List action method of ProductController.

Use the RedirectToRoute() Method

We can also use the RedirectToRoute() method to call an action method within the same controller or a different controller. This method needs a name of the route and route values as arguments to execute. The method looks for a specific route from the Route table and then redirects to the specific controller and action.

So, to use RedirectToRoute() method first we will define the route in the Program class:

app.MapControllerRoute(
    "productlist",
    "Product/List",
    new { controller = "Product", action = "List", id = UrlParameter.Optional }
);

Here, productlist is the name of the route, Product/List is the route pattern. The third argument is called  defaults which provides the controller name, action name, and an optional parameter.

So, to execute this route, we use the RedirectToRoute() method:

public class HomeController : Controller
{
    public IActionResult OurProduct()
    {
        return RedirectToRoute("productlist");
    }
}

Here, we pass the productlist as an argument to the RedirectToRoute() method, which is the name of the route. With this, we call the List() action of ProductController from the OurProduct action of HomeController.

Alternatively, we can directly define the route in the action method using the RedirectToRoute() method:

public class HomeController : Controller
{
    public IActionResult FetchProduct()
    {
        return RedirectToRoute(new
        {
            controller = "Product",
            action = "List"
        });
    }
}

This way, we call the List() action of ProductController class with the help of RedirectToRoute() method by passing an anonymous object.

Using Additional Arguments With the RedirectToRoute() Method

Let’s see how we can pass some additional arguments to the RedirectToRoute() method:

public IActionResult FetchProductById(int productId)
{
    return RedirectToRoute(new
    {
        controller = "Product",
        action = "GetProductById",
        id = productId
    });
}

We create the FetchProductById()action that has oneproductId parameter.

In our RedirectToRoute() method, we include an id property in our anonymous object, which we can retrieve in the other action:

public IActionResult GetProductById(int id)
{
    ViewData["id"] = id;

    return View();
}

Here, we use ViewData to retrieve our id parameter, returning the View associated with this action method.

Conclusion

In this article, we learned how to call a controller action method from another controller.

We also learned about a couple of methods to achieve that.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!