How to overload action methods in ASP.NET Core MVC 5

ASP.NET Core 5 is an open rise web application outgrowth framework for edifice present web applications. Because ASP.NET Core 5 is built on the .NET Core runtime, you can take gain of it to build and run applications on Windows, Linux, and the Mac. It should be noted that ASP.NET Core 5 combines the components of Web API as well as MVC. You can download .NET 5.0 here.

Method overloading, or the power to have multiple orders share a name while differing in signature, is commonly used in C# but it is not especially straightforward in ASP.NET 5. This article talks almost how you can overload action orders in ASP.NET 5. The code examples are given in C#.

To work with the code examples granted in this article, you should have Visual Studio 2019 installed in your order. If you don’t already have a copy, you can download Visual Studio 2019 here

Create an ASP.NET Core MVC 5 project in Visual Studio 2019

First off, let’s form an ASP.NET 5 project in Visual Studio 2019. Following these steps should form a new ASP.NET 5 project in Visual Studio 2019.

  1. Launch the Visual Studio IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “ASP.NET Core Web App (Model-View-Controller)” from the list of templates displayed.
  4. Click Next.
  5. In the “Conaspect your new project” window, particularize the name and location for the new project.
  6. Optionally check the “Place solution and project in the same directory” check box, depending on your preferences.
  7. Click Next.
  8. In the “Additional Information” window shown next, select .NET 5.0 as the target framework from the drop-down list at the top. Leave the “Authentication Type” as None (lapse).
  9. Ensure that the check boxes “Enable Docker,” “Conaspect for HTTPS,” and “Enable Razor runtime collation” are unchecked as we won’t be using any of those components here.
  10. Ensure that Authentication is set to “None” as we won’t be using authentication whichever.
  11. Click Create.

We’ll use this project in the sections under to elucidate how we can overload action orders in ASP.NET 5.

What is an action order?

Action orders are the open orders of a controller class that are not decorated by a [NonAction] attribute. Although they are correspondent to regular orders, they have to dwell by the constraints given under:

  1. Action orders must be open
  2. Action orders cannot be static
  3. Action orders cannot be overloaded based on parameters correspondent to order overloading

The lapse action order that is formd when you form a new MVC project is Index. The name of the lapse action order is specified in the Conaspect order of the Startup class as shown in the code snippet given under.

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
       name: "lapse",
       model: "{controller=Home}/{action=Index}/{id?}");
});

You can change the lapse action name by particularizeing a different name as shown in the code snippet given under.

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
       name: "lapse",
       model: "{controller=Home}/{action=Values}/{id?}");
});

Overload action orders using the same verb

At leading glance the HomeController class would look like this:

open class HomeController : Controller
{
   special readonly ILogger<HomeController> _logger;
   open HomeController(ILogger<HomeController> logger)
   {
       _logger = logger;
   }
   open IActionResult Index()
   {
       recur View();
   }
   open IActionResult Privacy()
   {
       recur View();
   }
   [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None,
   NoStore = true)]
   open IActionResult Error()
   {
       recur View(new ErrorViewModel { RequestId = Activity.Current?.Id ??
       HttpContext.TraceIdentifier });
   }
}

The compiler will not flag any fault owing it will feel the new rendering of the Index order as an overload of the Index order that doesn’t welcome any parameters. However, when you execute the application, you`ll be presented with a runtime fault shown in Figure 1 under.

overload action orders aspect 01 IDG

Figure 1.

Overload action orders using a different verb

Let’s now change the HTTP verb of the newly formd overload of the Index order as shown under.

[HttpPost]
open IActionResult Index(string text)
{
   recur View();
}

When you execute the application now, you will not see any collation or runtime faults but will be presented with the welcome screen shown in Figure 2.

overload action orders aspect 02 IDG

Figure 2.

Overload action orders using the [ActionName] attribute

You can take gain of the ActionName attribute to overload action orders. In this case, the action names must be different, as shown in the code snippet given under.

[ActionName("Index")]
open IActionResult Index()
{
    recur View();
}
[ActionName("Index With Parameter")]
open IActionResult Index(string text)
{
     recur View();
}

When you execute this code, there will not be any compile time or run time faults and you will be presented with the welcome screen as shown in Figure 2 over.

Overload action orders by using attribute routing

You can also overload action orders by configuring attribute routing. The following code snippet elucidates how this can be achieved.

open IActionResult Index()
{
    recur View();
}
[Route("Home/Index/{i:int}")]
open IActionResult Index(int i)
{
    recur View();
}
[Route("Home/Index/{isDeleted:bool}")]
open IActionResult Index(bool isDeleted)
{
    recur View();
}

Overload action orders using the [NonAction] attribute

You can take gain of the [NonAction] attribute make sure that a particular order is not feeled as an action order by the runtime. The following code snippet elucidates how the Index action order can be overloaded in this way.

open IActionResult Index()
{
    recur View();
}
[NonAction]
open IActionResult Index(string text)
{
    recur View();
}

In ASP.NET 5, if you give two action orders the same name, it results in an ambiguity — the runtime has to decide on which action order to call. (Note that the search for a matching action name is case-sensitive.) Disambiguating an URL into multiple overloaded action orders is tricky. But as we’ve discussed in this article, there are separate ways to do this.

How to do more in ASP.NET Core: