New Services
When a service author creates new services that consider API versioning upfront, then the configuration and setup is very straightforward. The following examples provide a quick start setup for the respective platforms with default configurations.
API versions can be expressed with .NET attributes or by configured conventions. These examples all use .NET attributes. If you’re interested in using conventions instead, please review the API version conventions topic.
Minimal API
var builder = WebApplication.CreateBuilder( args );
builder.Services.AddProblemDetails();
builder.Services.AddApiVersioning();
var app = builder.Build();
var people = app.NewVersionedApi();
people.MapGet( "/people", () => new[] { new Person() } ).HasApiVersion( 1.0 );
app.Run();
MVC (Core)
[ApiVersion( 1.0 )]
[ApiController]
[Route( "[controller]" )]
public class PeopleController : ControllerBase
{
[HttpGet]
public IActionResult Get() => Ok( new[] { new Person() } );
}
var builder = WebApplication.CreateBuilder( args );
builder.Services.AddControllers();
builder.Services.AddProblemDetails();
builder.Services.AddApiVersioning().AddMvc();
var app = builder.Build();
app.MapControllers();
app.Run();
OData
[ApiVersion( 1.0 )]
public class PeopleController : ODataController
{
[EnableQuery]
public IActionResult Get() => Ok( new[] { new Person() } );
}
var builder = WebApplication.CreateBuilder( args );
builder.Services.AddControllers().AddOData();
builder.Services.AddProblemDetails();
builder.Services.AddApiVersioning().AddOData(
options =>
{
options.ModelBuilder.DefaultModelConfiguration = ( builder, apiVersion, routePrefix ) =>
{
builder.EntitySet<Person>( "People" );
};
options.AddRouteComponents();
} );
var app = builder.Build();
app.MapControllers();
app.Run();