Skip to content

sandip-Kalsariya/API-Versioning

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Mastering .NET Core API Versioning: Build Future-Proof, Scalable APIs

🧠 Introduction

APIs are the backbone of digital transformation, but what happens when your business evolves and your API needs to change? Without a solid versioning strategy, you risk breaking client integrations and losing trust. In this article, I’ll show you how to master API versioning in .NET Core β€” ensuring your APIs remain robust, flexible, and future-ready.


❓ What is API Versioning?

APIs are contracts. Once published, clients depend on them. As your product grows, so do the demands on your API. Versioning lets you:

  • Evolve your API without breaking existing clients.
  • Support multiple versions simultaneously, giving clients time to migrate.
  • Experiment with new features or designs safely.

πŸ“Š According to a 2024 Postman survey, 89% of developers consider versioning essential for API reliability and client satisfaction.


1️⃣ API Versioning Strategies in .NET Core

.NET Core supports several versioning strategies via the Asp.Versioning.Mvc.ApiExplorer package.

βœ… Install the NuGet Package

dotnet add package Asp.Versioning.Mvc.ApiExplorer

Screenshot 2025-07-05 130856

1. URL Path Versioning:

Example: /api/v1/products

  • Most visible & cache-friendly – the version lives in the route.
// Program.cs
services.AddApiVersioning(o =>
{
    o.DefaultApiVersion = new ApiVersion(1, 0);
    o.AssumeDefaultVersionWhenUnspecified = true;
    o.ReportApiVersions = true;
    o.ApiVersionReader = new UrlSegmentApiVersionReader(); // πŸ‘ˆ key line
});

// Controllers
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/products")]
[ApiController]
public class ProductsController : ControllerBase
{
    [HttpGet]
    public IActionResult Get()
    { 
        return Ok(new { version = "1.0", data = "Old SKU list" });
    }
}

[ApiVersion("2.0")]
[Route("api/v{version:apiVersion}/products")]
[ApiController]
public class ProductsV2Controller : ControllerBase
{
    [HttpGet]
    public IActionResult Get()
    {
       return Ok(new { version = "2.0", data = "New SKU list" });
    }
}

Request examples:

  • GET /api/v1/products β†’ returns v1 payload
  • GET /api/v2/products β†’ returns v2 payload

2. Query String Versioning:

Example: /api/products?api-version=1.0

  • Keeps URLs clean but less discoverable.
// Program.cs
services.AddApiVersioning(o =>
{
    o.DefaultApiVersion = new ApiVersion(1, 0);
    o.AssumeDefaultVersionWhenUnspecified = true;
    o.ReportApiVersions = true;
    o.ApiVersionReader = new QueryStringApiVersionReader("api-version");
});

// One controller, two actions mapped to versions
[Route("api/products")]
[ApiController]
public class ProductsController : ControllerBase
{
    [HttpGet]
    [MapToApiVersion("1.0")]
    public IActionResult GetV1()
    { 
        return Ok(new { version = "1.0", data = "Old SKU list" });
    }

    [HttpGet]
    [MapToApiVersion("2.0")]
    public IActionResult GetV2()
    {
        return Ok(new { version = "2.0", data = "New SKU list" });
    }
}

Request examples:

  • GET /api/products?api-version=1.0
  • GET /api/products?api-version=2.0

3. Header Versioning:

Example: Header: api-version: 1.0

  • RESTful and clean but requires client setup.
// Program.cs
services.AddApiVersioning(o =>
{
    o.DefaultApiVersion = new ApiVersion(1, 0);
    o.AssumeDefaultVersionWhenUnspecified = true;
    o.ReportApiVersions = true;
    o.ApiVersionReader = new HeaderApiVersionReader("api-version"); // πŸ‘ˆ
});

// Same controller technique as query-string example
[Route("api/products")]
[ApiController]
public class ProductsController : ControllerBase
{
    [HttpGet]
    [MapToApiVersion("1.0")]
    public IActionResult GetV1() 
    {
	return Ok(new { version = "1.0" });
    }

    [HttpGet]
    [MapToApiVersion("2.0")]
    public IActionResult GetV2()
    {	
	return Ok(new { version = "2.0" });
    }
}

Request examples:

  • GET /api/products with header api-version: 1.0
  • GET /api/products with header api-version: 2.0

4. Media Type Versioning:

Example: Accept: application/json;v=1.0

  • Powerful for content negotiation.
// Program.cs
services.AddApiVersioning(o =>
{
    o.DefaultApiVersion = new ApiVersion(1, 0);
    o.AssumeDefaultVersionWhenUnspecified = true;
    o.ReportApiVersions = true;
    o.ApiVersionReader = new MediaTypeApiVersionReader("v");  // πŸ‘ˆ
});

[Route("api/products")]
[ApiController]
public class ProductsController : ControllerBase
{
    [HttpGet]
    [MapToApiVersion("1.0")]
    public IActionResult GetV1() 
    {
	return Ok(new { version = "1.0" });
    }

    [HttpGet]
    [MapToApiVersion("2.0")]
    public IActionResult GetV2()
    {	
	return Ok(new { version = "2.0" });
    }
}

Request examples:

  • GET /api/products with Accept: application/json;v=1.0
  • GET /api/products with Accept: application/json;v=2.0

πŸ“ˆ 2. Pros and Cons of Versioning Strategies

Approach Pros Cons
URL Path Easy to use, visible, cacheable Can clutter routes
Query String Clean URLs, easy to implement Harder to cache, less visible
Header RESTful, no URL changes Requires client changes
Media Type Flexible, supports negotiation Complex, less common

🧾 3. Quick Comparison

Strategy Visibility RESTful Client Config Needed Cache Friendly
URL Path βœ… High βœ… Yes ❌ No βœ… Yes
Query String ⚠️ Medium βœ… Yes ❌ No ⚠️ Partial
Header ❌ Low βœ… Yes βœ… Yes βœ… Yes
Media Type ❌ Low βœ… Yes βœ… Yes ⚠️ Partial

πŸ–ΌοΈ 4. Visuals/Images: Understanding API Versioning at a Glance

Visuals simplify complex concepts. Here’s a diagram to help you visually understand how clients interact with different API versions through an API Gateway in a .NET Core environment:

Key Elements:

  • πŸ‘₯ Clients: Web, Mobile

  • πŸšͺ API Gateway

  • πŸ”€ Routes:

    • /api/v1/products
    • /api/v2/products
    • /api/v3/products
  • 🧭 Versioning Strategies:

    • 🌐 URL Path
    • ❓ Query String
    • πŸ“© Header
    • πŸ“° Media Type

Screenshot 2025-07-05 133733

Screenshot 2025-07-05 133811

Screenshot 2025-07-05 133844


πŸ§ͺ 5. Real-World Example: Versioning at Scale in a Growing SaaS Platform

A mid-sized SaaS company built their backend using .NET Core Web API. As their customer base grew, so did the demands for new featuresβ€”but without breaking existing integrations for long-time users.

πŸ” Challenge: Early on, they had only one API version (v1). Every changeβ€”new features, response structure updatesβ€”risked breaking production clients.

βœ… Solution:

They implemented URL-based API versioning (/api/v1/, /api/v2/) using .NET Core API Versioning library. New features were added in v2, while v1 remained stable for legacy clients. They gradually rolled out v2 with a sunset strategy for v1.

πŸ“ˆ Results:

  • βœ… 0% downtime during rollout
  • πŸš€ 70% client migration to v2 within 6 months
  • πŸ‘¨β€πŸ’» Increased developer productivity by isolating logic per version

This strategy gave them the flexibility to evolve their APIs without fear of regressionsβ€”future-proofing their system as they scaled.

πŸ“Š 6. Data/Statistics: Why API Versioning Matters

  • πŸ” 83% of developers say managing breaking changes is a top challenge
  • 🚨 APIs without versioning are 3x more likely to cause client failures
  • πŸ§ͺ Structured versioning reduces integration issues by 40%+
  • πŸ“ˆ Improves API lifecycle management and developer experience

πŸ’¬ Let's Talk!

πŸ”„ How are you handling API changes in your projects?

πŸ€” Faced any challenges with versioning?

πŸ‘‡ Share your experiences in the comments β€” or 🀝 connect with me to discuss best practices and real-world solutions!

βœ… Ready to make your APIs future-proof?

πŸš€ Start implementing versioning in your .NET Core projects today and build with confidence for tomorrow! πŸ’‘


πŸ“‚ GitHub Project

Want to explore the code? Dive into the complete .NET Core API Versioning project here: πŸ”— https://github.com/sandip-Kalsariya/API-Versioning

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages