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.
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.
We’ll use this project in the sections under to elucidate how we can overload action orders in ASP.NET 5.
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:
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?}");
});
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.
Figure 1.
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.
Figure 2.
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.
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();
}
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.