Skip to content

mphiliseni/API-Versioning-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ASP.NET Core API Versioning Demo

A comprehensive demonstration of API versioning in ASP.NET Core using URL path versioning with Swagger documentation.

πŸš€ Features

  • URL Path Versioning: Clean versioning using URL segments (e.g., /api/v1.0/Hello, /api/v2.0/Hello)
  • Swagger Integration: Interactive API documentation with version-specific endpoints
  • Version Discovery: Automatic API version reporting in response headers
  • Deprecation Support: Mark older API versions as deprecated
  • Multiple Controllers: Separate controllers for different API versions
  • Clean Architecture: Well-organized project structure

πŸ› οΈ Technology Stack

  • .NET 9.0: Latest .NET framework
  • ASP.NET Core: Web API framework
  • Microsoft.AspNetCore.Mvc.Versioning: API versioning library
  • Swashbuckle.AspNetCore: Swagger/OpenAPI documentation
  • Microsoft.AspNetCore.OpenApi: OpenAPI specification support

πŸ“‹ Prerequisites

  • .NET 9.0 SDK
  • Visual Studio Code, Visual Studio, or any preferred IDE
  • Git (for cloning the repository)

πŸƒβ€β™‚οΈ Quick Start

1. Clone the Repository

git clone https://github.com/mphiliseni/API-Versioning-.git
cd API-Versioning-/APIVersion

2. Restore Dependencies

dotnet restore

3. Build the Project

dotnet build

4. Run the Application

dotnet run

The API will be available at https://localhost:5238 (or the port shown in the console output).

πŸ“– API Endpoints

Version 1.0 (Deprecated)

  • GET /api/v1.0/Hello - Returns greeting from version 1.0
  • Response: "πŸ‘‹ Hello API - Version 1"

Version 2.0 (Current)

  • GET /api/v2.0/Hello - Returns greeting from version 2.0
  • Response: "πŸ‘‹ Hello API - Version 2"

Documentation

  • Swagger UI: /swagger - Interactive API documentation
  • OpenAPI Spec: /swagger/{version}/swagger.json - OpenAPI specification for each version

πŸ”§ Configuration

API Versioning Setup

The API versioning is configured in Program.cs:

builder.Services.AddApiVersioning(options =>
{
    options.DefaultApiVersion = new ApiVersion(1, 0);
    options.AssumeDefaultVersionWhenUnspecified = true;
    options.ReportApiVersions = true;
    options.ApiVersionReader = new UrlSegmentApiVersionReader();
}).AddVersionedApiExplorer(options =>
{
    options.GroupNameFormat = "'v'VVV";
    options.SubstituteApiVersionInUrl = true;
});

Controller Versioning

Controllers are versioned using attributes:

[ApiVersion("1.0", Deprecated = true)]  // V1 - Deprecated
[ApiVersion("2.0")]                     // V2 - Current
[Route("api/v{version:apiVersion}/[controller]")]

πŸ“ Project Structure

APIVersion/
β”œβ”€β”€ Controllers/
β”‚   β”œβ”€β”€ V1/
β”‚   β”‚   └── HelloController.cs    # Version 1.0 controller (deprecated)
β”‚   └── V2/
β”‚       └── HelloController.cs    # Version 2.0 controller
β”œβ”€β”€ Options/
β”‚   └── ConfigureSwaggerOptions.cs # Swagger configuration
β”œβ”€β”€ Properties/
β”‚   └── launchSettings.json       # Launch configuration
β”œβ”€β”€ Program.cs                    # Application startup
β”œβ”€β”€ ApiVersion.csproj            # Project file
└── appsettings.json             # Application settings

πŸ§ͺ Testing the API

Using curl

# Test Version 1.0
curl -X GET "https://localhost:5238/api/v1.0/Hello"

# Test Version 2.0
curl -X GET "https://localhost:5238/api/v2.0/Hello"

# Check API versions (look for api-supported-versions header)
curl -I "https://localhost:5238/api/v2.0/Hello"

Using Swagger UI

  1. Navigate to https://localhost:5238/swagger
  2. Select the API version from the dropdown
  3. Expand the endpoint and click "Try it out"
  4. Execute the request to see the response

πŸ” Key Features Explained

Version Discovery

The API reports supported versions in the api-supported-versions response header, making it easy for clients to discover available versions.

Deprecation Handling

Version 1.0 is marked as deprecated using the Deprecated = true parameter, which will be reflected in the API documentation and response headers.

URL Path Versioning

This implementation uses URL path versioning (/api/v{version}/controller), which is:

  • SEO Friendly: Easy to cache and index
  • Clear: Version is explicitly visible in the URL
  • Routing Friendly: Works well with ASP.NET Core routing

Swagger Integration

Each API version gets its own Swagger document, allowing for:

  • Version-specific documentation
  • Different schemas per version
  • Clear separation of concerns

πŸš€ Deployment

Development

dotnet run --environment Development

Production

dotnet publish -c Release -o ./publish

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ“š Additional Resources

πŸ› Troubleshooting

Port Already in Use

If you get a "port already in use" error:

# Find the process using the port
lsof -i :5238

# Kill the process (replace PID with actual process ID)
kill -9 <PID>

Build Errors

Ensure all NuGet packages are restored:

dotnet clean
dotnet restore
dotnet build

Made with ❀️ using ASP.NET Core and API Versioning

About

Implement API Versioning in ASP.NET Core Web API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages