Routing in Web API – Extended Guide with Real-World Examples

📌 Routing in Web API – Extended Guide with Real-World Examples

🔍 Overview

Routing is the heart of a Web API. It determines how incoming HTTP requests are mapped to the correct controller and action method. In a real application, proper routing helps organize endpoints, manage versions, and keep APIs intuitive.



📁 Real Project Folder Structure Example

Here’s how a typical ASP.NET Core Web API project with attribute routing and versioning might look:

MyApp.API/ ├── Controllers/ │ ├── v1/ │ │ └── ProductsController.cs │ ├── v2/ │ │ └── ProductsController.cs ├── Program.cs ├── Startup.cs (or Program.cs if using .NET 6+)

🔢 Versioned Routing

Versioning is useful when APIs evolve and newer clients require updated logic or data formats.

Example: Versioned Attribute Routing

namespace MyApp.API.Controllers.v1
{
    [ApiController]
    [Route("api/v1/[controller]")]
    public class ProductsController : ControllerBase
    {
        [HttpGet("{id}")]
        public IActionResult GetProductV1(int id)
        {
            return Ok($"Product {id} from V1");
        }
    }
}
namespace MyApp.API.Controllers.v2
{
    [ApiController]
    [Route("api/v2/[controller]")]
    public class ProductsController : ControllerBase
    {
        [HttpGet("{id}")]
        public IActionResult GetProductV2(int id)
        {
            return Ok($"Product {id} from V2 with extra fields");
        }
    }
}

Requests:

  • GET /api/v1/products/10 → calls V1 controller
  • GET /api/v2/products/10 → calls V2 controller

🧩 Route Constraints

Route constraints allow filtering routes based on data type or pattern.

Example: Adding Type Constraints

[HttpGet("{id:int}")]
public IActionResult GetById(int id)
{
    return Ok($"Integer ID: {id}");
}

[HttpGet("{name:alpha}")]
public IActionResult GetByName(string name)
{
    return Ok($"Name: {name}");
}

Requests:

  • GET /api/products/123 → goes to GetById
  • GET /api/products/iphone → goes to GetByName

Common Route Constraints

Constraint Description Example
int Must be an integer {id:int}
guid Must be a GUID {id:guid}
alpha Only letters (A–Z) {name:alpha}
length Fixed string length {code:length(5)}
min/max Min/max numeric limits {age:min(18)}

✅ Clean Route Design Best Practices

  • Use nouns, not verbs: /api/products instead of /api/getproducts
  • Use plural resource names: /api/users, /api/orders
  • Use query parameters for filtering: /api/products?category=mobile&inStock=true
  • Maintain route consistency across versions
  • Prefer attribute routing over convention-based for clarity and control

📌 Summary

  • Routing connects HTTP requests to action methods.
  • Attribute routing is the modern, flexible approach.
  • Versioning helps maintain backward compatibility.
  • Constraints improve route matching precision.
  • Clean route design improves maintainability and usability.

Comments

Popular posts from this blog

Debouncing & Throttling in RxJS: Optimizing API Calls and User Interactions

Promises in Angular

Comprehensive Guide to C# and .NET Core OOP Concepts and Language Features