You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.8 KiB

using CoreAgent.Infrastructure.Options;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Versioning;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace CoreAgent.Infrastructure.Extensions
{
public static class ApiVersioningExtensions
{
public static IServiceCollection AddApiVersioningExtension(this IServiceCollection services, IConfiguration config)
{
var settings = config.GetSection("ApiVersioning").Get<ApiVersioningSettings>() ?? new ApiVersioningSettings();
services.AddApiVersioning(options =>
{
options.DefaultApiVersion = ApiVersion.Parse(settings.DefaultVersion);
options.AssumeDefaultVersionWhenUnspecified = settings.AssumeDefaultVersionWhenUnspecified;
options.ReportApiVersions = settings.ReportApiVersions;
options.ApiVersionReader = ApiVersionReader.Combine(
settings.ShowVersionInUrl ? new UrlSegmentApiVersionReader() : null,
settings.ShowVersionInQueryString ? new QueryStringApiVersionReader("api-version") : null,
settings.ShowVersionInHeader ? new HeaderApiVersionReader("api-version") : null,
settings.ShowVersionInMediaType ? new MediaTypeApiVersionReader("version") : null
);
});
services.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
return services;
}
public static IApplicationBuilder UseApiVersioningExtension(this IApplicationBuilder app)
{
// 可以在这里添加版本控制相关的中间件
return app;
}
}
}