-
Notifications
You must be signed in to change notification settings - Fork 703
New Services Quick Start
Chris Martinez edited this page Dec 29, 2022
·
12 revisions
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.
public static class WebApiConfig
{
public static void Configuration( HttpConfiguration configuration )
{
configuration.AddApiVersioning();
// remaining configuration omitted for brevity
}
}
[ApiVersion( 1.0 )]
[RoutePrefix( "People" )]
public class PeopleController : ApiController
{
[Route]
public IHttpActionResult Get() => Ok( new[] { new Person() } );
}
public static class WebApiConfig
{
public static void Configuration( HttpConfiguration configuration )
{
configuration.AddApiVersioning();
var modelBuilder = new VersionedODataModelBuilder( configuration )
{
DefaultModelConfiguration = ( builder, apiVersion, routePrefix ) =>
{
builder.EntitySet<Person>( "People" );
}
};
configuration.MapVersionedODataRoutes( "odata", null, modelBuilder );
// remaining configuration omitted for brevity
}
}
[ApiVersion( 1.0 )]
[ODataRoutePrefix( "People" )]
public class PeopleController : ODataController
{
[EnableQuery]
[ODataRoute]
public IHttpActionResult Get() => Ok( new[] { new Person() } );
}
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();
[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();
[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();
- Home
- Quick Starts
- Version Format
- Version Discovery
- Version Policies
- How to Version Your Service
- API Versioning with OData
- Configuring Your Application
- Error Responses
- API Documentation
- Extensions and Customizations
- Known Limitations
- FAQ
- Examples