Routing in ASP.Net Core

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:02:00

Convention-based Routing

Convention-based routing in ASP.NET Core allows you to define routing patterns and conventions based on controller and action names, simplifying the setup of routes without explicitly specifying each one.

Example

// Startup.cs: In ASP.NET Core 3.x and 5.x
app.UseEndpoints(endpoints =>
{
  endpoints.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
});
// Program.cs: In ASP.NET Core 6.x/7.x and later
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

Convention-based Routing Configuration & Mapping

This feature lets you configure and map route conventions in ASP.NET Core 5, allowing for customized URL structures and parameter mappings based on specific naming conventions.

Example

// Startup.cs: In ASP.NET Core 3.x and 5.x
app.UseEndpoints(endpoints =>
{
  endpoints.MapControllerRoute(
    name: "custom",
    pattern: "products/{action=Index}/{id?}",
    defaults: new { controller = "Product" });
});
// Program.cs: In ASP.NET Core 6.x/7.x and later
app.MapControllerRoute(
    name: "custom",
    pattern: "products/{action=Index}/{id?}",
    defaults: new { controller = "Product" });

Attribute Routing

Attribute routing in ASP.NET Core enables you to define routes directly on controller actions, providing more fine-grained control over URL patterns.

Example

// Controller.cs
[Route("[controller]/[action]")]
public class ProductsController : ControllerBase
{
  public IActionResult GetProducts()
  {
    // Action logic here
  }
}

Attribute Routing Tokens

Attribute routing tokens allow you to define route constraints within attribute-based routing, ensuring that parameters match specific criteria or patterns.

Example

// Controller.cs
[Route("[controller]/[action]")]
public class ProductsController : ControllerBase
{
  [HttpGet("{id:int}")]
  public IActionResult GetProduct(int id)
  {
    // Action logic here
  }
}

Mixed Routing

ASP.NET Core 5 supports a mixed routing approach, where you can combine convention-based routing and attribute routing within the same application, offering flexibility in route configuration.

Example

// Startup.cs
app.UseEndpoints(endpoints =>
{
  endpoints.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
  endpoints.MapControllers(); // Attribute-based routing
});

Route Constraints

Route constraints in ASP.NET Core 5 allow you to specify conditions that must be met for a route to match, enabling validation and restriction of parameter values.

Example

// Controller.cs
[Route("Product/GetProduct/{id:int}")]
public IActionResult GetProduct(int id)
{
  // Action logic here
}

Optional Parameters

Optional parameters in routing enable you to define route segments that are not required, making it possible to have more flexible route patterns in ASP.NET Core 5.

Example

// Controller.cs
[Route("Product/GetProduct/{id?}")]
public IActionResult GetProduct(int? id)
{
  // Action logic here
}

Default Values

Default values in routing allow you to specify default parameter values for routes, simplifying route configuration and ensuring a graceful fallback.

Example

// Controller.cs
[Route("Product/GetProducts/{id:int?}")]
public IActionResult GetProduct(int id = 1)
{
  // Action logic here
}
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this