Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion samples/aspnetcore/BasicSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using System.Linq;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Mvc.Routing;

public class Startup
{
public Startup( IHostingEnvironment env )
Expand All @@ -27,7 +29,7 @@ public Startup( IHostingEnvironment env )
public void ConfigureServices( IServiceCollection services )
{
services.AddMvc();

// reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
services.AddApiVersioning( o => o.ReportApiVersions = true );
}
Expand All @@ -37,6 +39,7 @@ public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILogger
loggerFactory.AddConsole( Configuration.GetSection( "Logging" ) );
loggerFactory.AddDebug();
app.UseMvc();
app.UseApiVersioning();
}
}
}
1 change: 1 addition & 0 deletions samples/aspnetcore/ByNamespaceSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILogger
}

app.UseMvc();
app.UseApiVersioning();
}
}
}
1 change: 1 addition & 0 deletions samples/aspnetcore/ConventionsSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILogger
loggerFactory.AddConsole( Configuration.GetSection( "Logging" ) );
loggerFactory.AddDebug();
app.UseMvc();
app.UseApiVersioning();
}
}
}
1 change: 1 addition & 0 deletions samples/aspnetcore/SwaggerSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILogger
loggerFactory.AddDebug();

app.UseMvc();
app.UseApiVersioning();
app.UseSwagger();
app.UseSwaggerUI(
options =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using ApplicationModels;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using Versioning;
Expand All @@ -18,6 +19,24 @@ public static class ActionDescriptorExtensions

static void HasAggregatedVersions( this ActionDescriptor action, bool value ) => action.Properties[VersionsAggregated] = value;

internal static void AggregateAllVersions( this ActionDescriptor action, IEnumerable<ActionDescriptor> matchingActions )
{
Contract.Requires( action != null );
Contract.Requires( matchingActions != null );

if ( action.HasAggregatedVersions() )
{
return;
}

action.HasAggregatedVersions( true );

var model = action.GetProperty<ApiVersionModel>();
Contract.Assume( model != null );

action.SetProperty( model.Aggregate( matchingActions.Select( a => a.GetProperty<ApiVersionModel>() ).Where( m => m != null ) ) );
}

internal static void AggregateAllVersions( this ActionDescriptor action, ActionSelectionContext context )
{
Contract.Requires( action != null );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Microsoft.AspNetCore.Mvc
{
using Http;
using Microsoft.AspNetCore.Mvc.Routing;
using System;
using System.Diagnostics.Contracts;
using Versioning;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Microsoft.Extensions.DependencyInjection
{
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc.Routing;
using System;
using System.Diagnostics.Contracts;

/// <summary>
/// Provides extension methods for the <see cref="IApplicationBuilder"/> interface.
/// </summary>
[CLSCompliant( false )]
public static class IApplicationBuilderExtensions
{
/// <summary>
/// Adds API versioning to the <see cref="IApplicationBuilder"/> request execution pipeline.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder">application builder</see> to add API versioning to.</param>
/// <returns>The original <see cref="IApplicationBuilder"/>.</returns>
public static IApplicationBuilder UseApiVersioning( this IApplicationBuilder app )
{
Arg.NotNull( app, nameof( app ) );
Contract.Ensures( Contract.Result<IApplicationBuilder>() != null );

app.UseMvc( builder => builder.Routes.Add( builder.ServiceProvider.GetRequiredService<IApiVersionRoutePolicy>() ) );
return app;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ public static IServiceCollection AddApiVersioning( this IServiceCollection servi
setupAction( options );
services.Add( new ServiceDescriptor( typeof( IApiVersionReader ), options.ApiVersionReader ) );
services.Add( new ServiceDescriptor( typeof( IApiVersionSelector ), options.ApiVersionSelector ) );
services.Add( new ServiceDescriptor( typeof( IErrorResponseProvider ), options.ErrorResponses ) );
services.Add( Singleton<IOptions<ApiVersioningOptions>>( new OptionsWrapper<ApiVersioningOptions>( options ) ) );
services.Replace( Singleton<IActionSelector, ApiVersionActionSelector>() );
services.TryAddSingleton<IApiVersionRoutePolicy, DefaultApiVersionRoutePolicy>();

if ( options.ReportApiVersions )
{
Expand All @@ -58,7 +60,7 @@ public static IServiceCollection AddApiVersioning( this IServiceCollection servi
mvcOptions.Conventions.Add( new ApiVersionConvention( options.DefaultApiVersion, options.Conventions ) );
} );

services.AddRouting( mvcOptions => mvcOptions.ConstraintMap.Add( "apiVersion", typeof( ApiVersionRouteConstraint ) ) );
services.AddRouting( routeOptions => routeOptions.ConstraintMap.Add( "apiVersion", typeof( ApiVersionRouteConstraint ) ) );

return services;
}
Expand Down
Loading