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#.
app.UseEndpointsendpoints _gt;{ endpoints.MapControllerRoute name: "lapse"_ model: "{controller_Home}_{action_Index}_{id?}"; };<_pre> You can change the lapse action name by particularizeing a different name as shown in the code snippet given under.
<_aside> app.UseEndpointsendpoints _gt;{ endpoints.MapControllerRoute name: "lapse"_ model: "{controller_Home}_{action_Values}_{id?}"; };<_pre> Overload action orders using the same verb
At leading glance the HomeController class would look like this:
open class HomeController : Controller{ special readonly ILoggerlt;HomeControllergt; _logger; open HomeControllerILoggerlt;HomeControllergt; logger { _logger _ logger; } open IActionResult Index { recur View; } open IActionResult Privacy { recur View; } [ResponseCacheDuration _ 0_ Location _ ResponseCacheLocation.None_ NoStore _ true] open IActionResult Error { recur Viewnew ErrorViewModel { RequestId _ Activity.Current?.Id ?? HttpContext.TraceIdentifier }; } }<_pre> 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 doesnt welcome any parameters. However_ when you execute the application_ youll be presented with a runtime fault shown in Figure 1 under.
![]()
IDG<_little> Figure 1.
<_figcaption> <_aspect>Overload action orders using a different verb
Lets now change the HTTP verb of the newly formd overload of the Index order as shown under.
[HttpPost]open IActionResult Indexstring text { recur View; }<_pre> 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.
<_aside> ![]()
IDG<_little> Figure 2.
<_figcaption> <_aspect>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 Indexstring text { recur View; }<_pre> 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 Indexint i { recur View; } [Route"Home_Index_{isDeleted:bool}"] open IActionResult Indexbool isDeleted { recur View; }<_pre> 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 Indexstring text { recur View; }<_pre> 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 weve discussed in this article_ there are separate ways to do this.
How to do more in ASP.NET Core:
- How to use multiple instrumentations of an interface in ASP.NET Core<_li>
- How to use IHttpClientFactory in ASP.NET Core<_li>
- How to use the ProblemDetails middleware in ASP.NET Core<_li>
- How to form way constraints in ASP.NET Core<_li>
- How to feel user secrets in ASP.NET Core<_li>
- How to build gRPC applications in ASP.NET Core<_li>
- How to redirect a request in ASP.NET Core<_li>
- How to use attribute routing in ASP.NET Core<_li>
- How to pass parameters to action orders in ASP.NET Core MVC<_li>
- How to use API Analyzers in ASP.NET Core<_li>
- How to use way data tokens in ASP.NET Core<_li>
- How to use API renderinging in ASP.NET Core<_li>
- How to use Data Transfer Objects in ASP.NET Core 3.1<_li>
- How to feel 404 faults in ASP.NET Core MVC<_li>
- How to use dependency injection in action filters in ASP.NET Core 3.1<_li>
- How to use the options model in ASP.NET Core<_li>
- How to use endpoint routing in ASP.NET Core 3.0 MVC<_li>
- How to ship data to Excel in ASP.NET Core 3.0<_li>
- How to use LoggerMessage in ASP.NET Core 3.0<_li>
- How to send emails in ASP.NET Core<_li>
- How to log data to SQL Server in ASP.NET Core<_li>
- How to schedule jobs using Quartz.NET in ASP.NET Core<_li>
- How to recur data from ASP.NET Core Web API<_li>
- How to format response data in ASP.NET Core<_li>
- How to use an ASP.NET Core Web API using RestSharp<_li>
- How to accomplish async operations using Dapper<_li>
- How to use component flags in ASP.NET Core<_li>
- How to use the FromServices attribute in ASP.NET Core<_li>
- How to work with cookies in ASP.NET Core<_li>
- How to work with static files in ASP.NET Core<_li>
- How to use URL Rewriting Middleware in ASP.NET Core<_li>
- How to instrument rate limiting in ASP.NET Core<_li>
- How to use Azure Application Insights in ASP.NET Core<_li>
- Using advanced NLog components in ASP.NET Core<_li>
- How to feel faults in ASP.NET Web API<_li>
- How to instrument global qualification handling in ASP.NET Core MVC<_li>
- How to feel null values in ASP.NET Core MVC<_li>
- Advanced renderinging in ASP.NET Core Web API<_li>
- How to work with worker services in ASP.NET Core<_li>
- How to use the Data Protection API in ASP.NET Core<_li>
- How to use conditional middleware in ASP.NET Core<_li>
- How to work with session state in ASP.NET Core<_li>
- How to write efficient controllers in ASP.NET Core<_li> <_ul>