ASP.NET Core Custom Routing – Examples & Best Practices

ASP.NET Core Custom Routing – Examples & Best Practices

Routing in ASP.NET Core determines how HTTP requests map to controller actions. In this blog, we’ll walk through a wide range of routing scenarios—from simple custom routes to attribute-based routing for Web APIs. All examples are compatible with .NET Core 6.0, 7.0, and 8.0.

✅ 1. Simple Custom Route

Map a custom URL to a controller and action:

app.MapControllerRoute(
    name: "aboutus",
    pattern: "about",
    defaults: new { controller = "Info", action = "About" }
);

Effect: Navigating to /about will call the About() action in InfoController.

✅ 2. Route with Parameter

app.MapControllerRoute(
    name: "blogpost",
    pattern: "blog/{id:int}",
    defaults: new { controller = "Blog", action = "Details" }
);

This defines a route that accepts an integer ID. Example: /blog/10

✅ 3. Nested Segments

app.MapControllerRoute(
    name: "productDetails",
    pattern: "store/products/{category}/{id?}",
    defaults: new { controller = "Product", action = "Show" }
);

Supports optional parameters like /store/products/electronics or /store/products/electronics/101.

✅ 4. Route with Default Values

app.MapControllerRoute(
    name: "news",
    pattern: "news/{category?}",
    defaults: new { controller = "News", action = "Index", category = "general" }
);

If no category is specified, general is used as default.

✅ 5. Route with Constraints

app.MapControllerRoute(
    name: "yearArchive",
    pattern: "archive/{year:int:min(2000):max(2025)}",
    defaults: new { controller = "Archive", action = "ByYear" }
);

Valid only for years between 2000 and 2025.

✅ 6. SEO-Friendly Route

app.MapControllerRoute(
    name: "seoRoute",
    pattern: "articles/{title}-{id:int}",
    defaults: new { controller = "Article", action = "Read" }
);

This allows URLs like /articles/aspnet-routing-101-5.

✅ 7. Attribute Routing for MVC

[Route("home")]
public class HomeController : Controller
{
    [Route("welcome")]
    public IActionResult Welcome() => Content("Welcome to Home!");
}

URL: /home/welcome

✅ 8. Attribute Routing with Parameters

[Route("products")]
public class ProductController : Controller
{
    [Route("{id:int}")]
    public IActionResult Details(int id) => Content($"Product ID: {id}");
}

✅ 9. Optional Parameters and Defaults

[Route("reports/{year:int?}/{month:int?}")]
public IActionResult ByDate(int year = 2025, int month = 1)
{
    return Content($"Year: {year}, Month: {month}");
}

✅ 10. Token Replacement

[Route("[controller]/[action]")]
public class ProfileController : Controller
{
    public IActionResult Info() => Content("This is the profile info");
}

This maps to /profile/info.

✅ 11. Attribute Routing in API Controllers

[Route("api/products")]
[ApiController]
public class ProductsController : ControllerBase
{
    [HttpGet]
    public IActionResult GetAll() => Ok("All products");

    [HttpGet("{id:int}")]
    public IActionResult GetById(int id) => Ok($"Product {id}");
}

✅ 12. API Versioning

[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/products")]
[ApiController]
public class ProductsController : ControllerBase
{
    [HttpGet]
    public IActionResult GetV1() => Ok("API V1");
}

Need Help?

Contact Sudipto Kumar Mukherjee – Microsoft Certified Trainer with 20+ years of experience in .NET, C#, ASP.NET Core.

  • Phone: +91-93318-97923
  • Location: Dum Dum, Kolkata
  • Email: supernova.software.solutions@gmail.com

Any Query / Enrollment Request



Google Review Testimonials

.NET Online Training
Average Rating: 4.9
Votes: 50
Reviews: 50