Routing in ASP.NET core 3.0 and What does it mean by Endpoint Routing?
Routing in ASP.NET core 3.0 and What does it means Endpoint Routing?
Routing in ASP.NET Core 3.0
Routing is the process of matching incoming path URL and corresponding to the action’s method in an application. Routes are described in applications and configured in Startup while starting app. A Route can extract the values form URL from the request and values can be used for processing request. By using routing information form applications, routing will be able to generate URL that maps to the endpoint.
Endpoint Routing
In Asp.net 2.2, Endpoint Routing was introduced and it was properly implemented in Asp.net core 3.0. Upgrading of older routing to Endpoint routing will improve the performance of the applications.
Endpoint routing helps to handle routing in different middleware system (like Blazor, MVC, Razor Pages, gRPC and SignalR). With Endpoint routing system works taking care of each and every part rather than traditional routing staying without caring other part.
In Asp.net core 3.0 project setting up MVC and razor pages is different than older one. Let’s take a look.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddRazorPages();
}
In older version we used AddMvc(), but now we have to write AddControllers (for API Routing) or AddContollersWithViews() to get services for the Views. Actual configuration of Endpoint routing is done in Configure. Which looks like:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//…
//…
//…
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
Endpoint routing creates route table covering different sets of middleware. In this way you can route to different sub system easily. Why to go to middleware for just loading Privacy page. Older routing gets much complex while using gRPC, Blazor etc. So, we can take middleware as a single which is provided by Endpoint routing rather than going to middleware parts.
There are two type of routing used for action methods:
- Conventional Routing
- Attribute Routing
Conventional Routing:
While creating asp.net core app with MVC using default template, default routing is configured which is known as Conventional Routing.
After creating new project with default template of ASP.NET core with MVC, configuration looks like:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
ASP.NET core 3.0 doesnot use UseMvc() rather it uses UseEndpoints() and also doesnot use routes.MapRoute rather it uses endpoints.MapControllerRoute() which create route by giving name default. MVC is defined in middleware in ASP.NET core 3.0.
This default template route pattern "{controller=Home}/{action=Index}/{id?}" matches the Index of HomeController. While starting the application if we put breakpoint to the Index of HomeController, we can see this method is executed by default. This is all because we have defined default values in endpoints map.
Let’s add a new controller ProductController and with action method ProductDetails() with optional parameter id and modify the default path.
ProductController.cs
public class ProductController : Controller
{
public IActionResult ProductDetails(int id)
{
ViewBag.ID = id;
return View();
}
}
ProductDetail.cshtml
@{
ViewData["Title"] = "ProductDetails";
int id = ViewBag.ID;
}
<h1>ProductDetails</h1>
Product ID : @id
Startup.cs
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Product}/{action=ProductDetails}/{id?}");
});
Now let’s run the application and see the output.
Hope you understood this section and let’s go to another section.
Attribute Routing:
When you define route for each method or controller then we write attribute above each method or controller then it is known as Attribute Routing. You can use both conventional and attribute routing together.
Now, let us modify ProductController:
[Route("")]
[Route("Product/ProductDetails/{id:int}")]
public IActionResult ProductDetails(int id)
{
ViewBag.ID = id;
return View();
}
Here we have defined path “ ” and "Product/ProductDetails/{id:int}" to execute this method. Like wise we can define GET method [HttpGet(/product)] or POST metnod [HttpPost(/productdetail)]. If we need to route URl to controller then we can use [Route("[controller]/[action]")] above controller.
If you want to know more about routing in asp.netcore 3.0, click here.
Also Read:
-
ASP.NET Core Data Protection using IDataProtectionProvider With Example
-
how to send push notifications from Firebase using ASP.NET CORE 3.0 with FirebaseAdmin SDK?
-
Export C# list into CSV (Comma Separated Values) in ASP.NET CORE 3.0 Web Application