DEV Community

Hassan Farooqui
Hassan Farooqui

Posted on

Routing in .Net Core

Routing is a mechanism that maps incoming requests to its corresponding end point.

There are two types of routing in dotnet core:

Convention based routing:
It is defined in startup or program.cs files and normally used in MVC applications.

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

Attribute Routing:
Apply on controllers using decorators such as [Route], [HttpGet] etc. It is more flexible than convention based routing. It is used in Restful services.

Example:
[Route("api/products")]
public class ProductsController : Controller
{
[HttpGet("{id}")]
public IActionResult GetProduct(int id) { ... }
}

Top comments (0)