ASP.NET Core MVC is the .NET Core match of the ASP.NET MVC framework. You can take gain of ASP.NET Core MVC to build cross-platform_ scalable_ high-accomplishment web applications and APIs using the Model-View-Controller design model. ASP.NET Core takes gain of routing to map incoming requests to relative controller actions.
You can acquire the basics of routing in ASP.NET Core from my earlier article_ “Demystified: Routing in ASP.NET Core.” This article presents a debateion of how we can use endpoint routing in ASP.NET Core 3.0 MVC.
In the Solution Explorer Window_ select the Controllers folder in the project_ right-click and then click on Add-gt;Controller… to form a new controller. Specify the name of the controller class as AuthorController. Next_ restore the code of the AuthorController class with the pursueing code.
[Route"api_[controller]"][ApiController] open class AuthorController : ControllerBase { readonly Repository repository _ new Repository; [HttpGet] open ActionResult GetAuthors { var records _ repository.GetAllAuthors; recur Okrecords; } }<_pre> Well use the AuthorController in the posterior sections of this article.
Understand routing in ASP.NET Core
Routing is a component that exposes endpoints and matches incoming HTTP requests to your controllers action orders. The routing middleware pertaining to the Microsoft.AspNetCore.Routing namespace is responsible for handling requests and responses_ inspecting requests and matching them to endpoints_ and even modifying the request and response messages that flow through the request processing pipeline.
Convention routing vs. attribute routing
You can particularize routing in your action orders in two different ways — assemblage-based routing and attribute-based routing.
The pursueing code snippet illustrates how you can use assemblage routing in the Configure order of the Startup class.
<_aside> app.UseMvcways _gt;{ ways.MapRoute name: "lapse"_ template: "{controller_Author}_{action_GetAuthors}_{id?}"; };<_pre> When using attribute-based routing you would mention way on your controller or the action order. You can mark multiple ways for the same action order. Here is an sample that illustrates this.
open class AuthorController : Controller{ [Route""] [Route"Home"] [Route"Home_Index"] open IActionResult Index { recur View; } [Route"Home_GetAuthor_{id:int}"] open IActionResult GetAuthorint id { ViewBag.Id _ id; recur View; } }<_pre> Route determinations in ASP.NET Core 3.0 MVC
When you form a new ASP.NET Core 3.0 MVC application_ a lapse way will be formd for you by Visual Studio. This is how it would look.
app.UseEndpointsendpoints _gt;{ endpoints.MapControllerRoute name: "lapse"_ model: "{controller_Home}_{action_Index}_{id?}"; }<_pre> The way determination shown here consists of two parameters — the name of the way and the way model. This way will match the pursueing URLs.
_Home_Index_Author_Index _Author_Index_12<_pre> UseRouting vs. UseEndpoints in ASP.NET Core 3.0 MVC
Routing takes gain of a pair of middleware components that are registered using the UseRouting and UseEndpoints extension orders. While the preceding is used to match a request to an endpoint_ the latter is used to execute a matched endpoint.
Note that the UseRouting middleware should be configured forward of all other middleware including authentication_ authorization_ and any manner middleware. By opposition_ the UseEndpoints middleware should be configured at the end. The pursueing code snippet illustrates this.
open void ConfigureIApplicationBuilder app_ IWebHostEnvironment env{ if env.IsDevelopment { app.UseDeveloperExceptionPage; } else { app.UseExceptionHandler"_Home_Error"; } app.UseStaticFiles; app.UseRouting; app.UseAuthorization; app.UseEndpointsendpoints _gt; { endpoints.MapControllerRoute name: "lapse"_ model: "{controller_Author}_{action_GetAuthor}_{id?}"; }; }<_pre> If you want to check the way metadata at runtime_ you can use the pursueing code and debug it in Visual Studio.
app.Useasync tenor_ next _gt;{ var endPoint _ tenor.GetEndpoint; var ways _ tenor.Request.RouteValues; };<_pre> Configure routing in ASP.NET Core 3.0 MVC
You can set up routing in your ASP.NET Core 3.0 MVC application easily. For this you would need to make a couple of changes to the Startup class. First off_ you should call the AddRazorPages order as shown under.
open void ConfigureServicesIServiceCollection labors{ labors.AddRazorPages; }<_pre> Next_ you should call the MapRazorPages order as shown under. This order when named will add the Razor Pages labors_ options_ and assemblages to the pipeline.
open void ConfigureIApplicationBuilder app_ IWebHostEnvironment env{ __Usual code app.UseEndpointsendpoints _gt; { endpoints.MapRazorPages; endpoints.MapControllerRoute name: "lapse"_ model: "{controller_Author}_ {action_GetAuthor}_{id?}"; }; }<_pre> Use named ways in ASP.NET Core 3.0 MVC
Note that you can particularize a name for a way so that you can use multiple ways having the same parameters. Such ways are known as named ways. The pursueing code snippet illustrates how named ways can be used.
app.UseEndpointsendpoints _gt;{ endpoints.MapControllerRoute name: "lapse"_ model: "{controller_Author}_{action_GetAuthors}_{id?}"; }<_pre> Set way lapses in ASP.NET Core 3.0 MVC
You can also explicitly set way lapses as shown in the code snippet under.
endpoints.MapControllerRoutename: "lapse"_ model: "{controller}_{action}_{id?}"_ lapses: new {controller _ "Home"_ action _ "Index"};<_pre> Use MapRazorPages in ASP.NET Core 3.0 MVC
You can take gain of the MapRazorPages extension order to empower routing for your Razor view pages. The pursueing code snippet shows how this can be achieved.
app.UseEndpointsendpoints _gt;{ endpoints.MapRazorPages; };<_pre> Use mixed routing in ASP.NET Core 3.0 MVC
Note that you can use attribute-based routing for some controllers and actions and assemblage-based routing on other controllers and action orders. Although it is fully strong to use both_ it should be noted that you cannot have attribute-based routing and assemblage-based routing in the same action order.
ASP.NET 3.0 gives us an extensible and modular framework for working with endpoints_ URLs_ and middleware. Ill debate routing in more depth in a forthcoming article here. Well explore the advanced routing components in ASP.NET Core 3.0 such as way templates_ way constraints_ manner way constraints_ and more.
How to do more in ASP.NET and ASP.NET Core:
- How to use in-memory caching in ASP.NET Core<_li>
- How to feel errors in ASP.NET Web API<_li>
- How to pass multiple parameters to Web API controller orders<_li>
- How to log request and response metadata in ASP.NET Web API<_li>
- How to work with HttpModules in ASP.NET<_li>
- Advanced versioning in ASP.NET Core Web API<_li>
- How to use dependency injection in ASP.NET Core<_li>
- How to work with sessions in ASP.NET<_li>
- How to work with HTTPHandlers in ASP.NET<_li>
- How to use IHostedService in ASP.NET Core<_li>
- How to use a WCF SOAP labor in ASP.NET Core<_li>
- How to better the accomplishment of ASP.NET Core applications<_li>
- How to use an ASP.NET Core Web API using RestSharp<_li>
- How to work with logging in ASP.NET Core<_li>
- How to use MediatR in ASP.NET Core<_li>
- How to work with session state in ASP.NET Core<_li>
- How to use Nancy in ASP.NET Core<_li>
- Understand parameter restrictive in ASP.NET Web API<_li>
- How to upload files in ASP.NET Core MVC<_li>
- How to instrument global qualification handling in ASP.NET Core Web API<_li>
- How to instrument health checks in ASP.NET Core<_li>
- Best practices in caching in ASP.NET<_li>
- How to use Apache Kafka messaging in .NET<_li>
- How to empower CORS on your Web API<_li>
- When to use WebClient vs. HttpClient vs. HttpWebRequest<_li>
- How to work with Redis Cache in .NET<_li>
- When to use Task.WaitAll vs. Task.WhenAll in .NET<_li> <_ul>