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

Class Guidelines for Effective 1-on-1 Learning

To keep every session productive and distraction-free, please follow these simple guidelines:

  • Quiet Environment: Join from a calm, private room with minimal background noise. Avoid public or noisy places.
  • No Interruptions: Inform family/roommates in advance. Keep doors closed during class.
  • Mobile on Silent / DND: Set your phone to Silent or Do Not Disturb to prevent calls and notifications.
  • Be Fully Present: Do not multitask. Avoid attending to other calls, visitors, or errands during the session.
  • Stable Setup: Use a laptop/desktop with a stable internet connection and required software installed (Visual Studio/.NET, SQL Server, etc.).
  • Punctuality: Join on time so we can utilize the full session effectively.
  • Prepared Materials (If any): Keep project files, notes, and questions ready for quicker progress.

Following these guidelines helps you focus better and ensures I can deliver the best learning experience in every class.

Schedule a Quick 10-Minute Call

I prefer to start with a short 10-minute free call so I can understand:

  • Your learning objectives and career goals
  • Your current skill level
  • The exact topics you want to learn

Why? Because course content, teaching pace, and fees all depend on your needs — there’s no “one-size-fits-all” pricing. Please leave your details below, and I’ll get back to you to arrange a convenient time for the call.



Google Review Testimonials

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