在Host项目的Module进行配置
1.PreConfigureServices
public override void PreConfigureServices(ServiceConfigurationContext context)
{
PreConfigure<AbpAspNetCoreMvcOptions>(options =>
{
//2.0 Version
options.ConventionalControllers.Create(typeof(AbpVersioningDemoModule).Assembly, opts =>
{
opts.TypePredicate = t => !string.IsNullOrEmpty(t.Namespace) && t.Namespace.Contains("Abp.Versioning.Demo.App.v2");
//opts.TypePredicate = t => t.Namespace == typeof(Abp.Versioning.Demo.App.AutoAppService).Namespace;
opts.ApiVersions.Add(new ApiVersion(2, 0));
});
//1.0 Compatibility version
options.ConventionalControllers.Create(typeof(AbpVersioningDemoModule).Assembly, opts =>
{
opts.TypePredicate = t => t.Namespace == typeof(Abp.Versioning.Demo.App.v1.TodoAppService).Namespace;
//opts.TypePredicate = t => t.Namespace == typeof(Abp.Versioning.Demo.App.AutoAppService).Namespace;
opts.ApiVersions.Add(new ApiVersion(1, 0));
});
});
}
2.ConfigureServices
public override void ConfigureServices(ServiceConfigurationContext context)
{
var preActions = context.Services.GetPreConfigureActions<AbpAspNetCoreMvcOptions>();
Configure<AbpAspNetCoreMvcOptions>(options =>
{
preActions.Configure(options);
});
context.Services.AddAbpApiVersioning(options =>
{
options.UseApiBehavior = false;
options.ReportApiVersions = true;
options.AssumeDefaultVersionWhenUnspecified = true;
//options.ApiVersionReader = new HeaderApiVersionReader("api-version"); //Supports header too
//options.ApiVersionReader = new MediaTypeApiVersionReader(); //Supports accept header too
options.ConfigureAbp(preActions.Configure());
});
context.Services.AddVersionedApiExplorer(
options =>
{
// add the versioned api explorer, which also adds IApiVersionDescriptionProvider service
// note: the specified format code will format the version as "'v'major[.minor][-status]"
options.GroupNameFormat = "'v'VVV";
// note: this option is only necessary when versioning by url segment. the SubstitutionFormat
// can also be used to control the format of the API version in route templates
options.SubstituteApiVersionInUrl = true;
});
Configure<AbpAspNetCoreMvcOptions>(options =>
{
options.ChangeControllerModelApiExplorerGroupName = false;
});
context.Services.AddAbpSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "API V1", Version = "v1", Description = "v1版本" });
options.SwaggerDoc("v2", new OpenApiInfo { Title = "API V2", Version = "v2", Description = "v2版本" });
options.DocInclusionPredicate((docName, description) =>
{
if (!description.TryGetMethodInfo(out MethodInfo method) || method.DeclaringType is null)
{
return false;
}
if (docName.Equals("v1") && !string.IsNullOrEmpty(method.DeclaringType.FullName) && method.DeclaringType.FullName.Contains(".v1"))
{
return true;
}
if (docName.Equals("v2") && !string.IsNullOrEmpty(method.DeclaringType.FullName) && method.DeclaringType.FullName.Contains(".v2"))
{
return true;
}
return false;
});
options.CustomSchemaIds(type => type.FullName);
});
context.Services.AddHttpClientProxies(typeof(AbpVersioningDemoModule).Assembly);
Configure<AbpRemoteServiceOptions>(options =>
{
options.RemoteServices.Default = new RemoteServiceConfiguration("/");
});
}
3.OnApplicationInitialization
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
var app = context.GetApplicationBuilder();
app.UseStaticFiles();
app.UseRouting();
app.UseSwagger();
app.UseAbpSwaggerUI(c =>
{
var provider = app.ApplicationServices.GetRequiredService<IApiVersionDescriptionProvider>();
// build a swagger endpoint for each discovered API version
foreach (var description in provider.ApiVersionDescriptions)
{
c.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
}
});
app.UseConfiguredEndpoints();
}