Can we hide ABP related HTTP API endpoints if desired?
Even for an empty project, we have the following pre-defined HTTP endpoints:

We may want to disable these endpoints (not only hide from swagger, but completely disable them). So, we need to provide an option for it.
I am hiding the abp endpoints on Swagger UI by adding a [ShowInSwagger] attribute to the controllers I want to see, then using this in my host module:
public class ShowInSwaggerAttributeFilter : IDocumentFilter
{
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
var filteredApis = context.ApiDescriptions.Where(a => a.CustomAttributes().Any(x =>
x.GetType() == typeof(ShowInSwaggerAttribute)));
foreach (var path in swaggerDoc.Paths.ToList())
{
if (filteredApis.All(x => ("/" + x.RelativePath) != path.Key))
swaggerDoc.Paths.Remove(path.Key);
}
}
}
How do I prevent the execution of certain methods and tell ABP to ignore them when creating endpoints (by convention)?
it is greate enhancement, recently I am looking for it .
I don’t want to expose some pre-built apis
Is there any update on this?
I am hiding the abp endpoints on Swagger UI by adding a
[ShowInSwagger]attribute to the controllers I want to see, then using this in my host module:public class ShowInSwaggerAttributeFilter : IDocumentFilter { public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) { var filteredApis = context.ApiDescriptions.Where(a => a.CustomAttributes().Any(x => x.GetType() == typeof(ShowInSwaggerAttribute))); foreach (var path in swaggerDoc.Paths.ToList()) { if (filteredApis.All(x => ("/" + x.RelativePath) != path.Key)) swaggerDoc.Paths.Remove(path.Key); } } }
How do I make this work? I added this class to my host module, but swagger doesnt run it (I tried setting a breakpoint)
@hikalkan @ArneCools ShowInSwaggerAttributeFilter must be added in .HttpApi.Host project configuration:
private static void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration)
{
context.Services.AddAbpSwaggerGenWithOAuth(
configuration["AuthServer:Authority"],
new Dictionary<string, string>
{
{"...", "... API"}
},
options =>
{
options.DocumentFilter<ShowInSwaggerAttributeFilter>(); // <- ADD THIS!
options.SwaggerDoc("v1", new OpenApiInfo {Title = "... API", Version = "v1"});
options.DocInclusionPredicate((docName, description) => true);
options.CustomSchemaIds(type => type.FullName);
});
}
@hikalkan any news?
private static void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration)
{
context.Services.AddAbpSwaggerGenWithOAuth(
configuration["AuthServer:Authority"],
new Dictionary<string, string>
{
{"...", "... API"}
},
options =>
{
options.SwaggerDoc("v1", new OpenApiInfo {Title = "... API", Version = "v1"});
//options.DocInclusionPredicate((docName, description) => true); // <- Comment out this line of code
options.CustomSchemaIds(type => type.FullName);
});
}
try it out
The following example hides the API in Volo.Abp.Account.HttpApi
public override void OnPreApplicationInitialization(ApplicationInitializationContext context)
{
var applicationPartManager = context.ServiceProvider.GetRequiredService<ApplicationPartManager>();
applicationPartManager.ApplicationParts.RemoveAll(s => s.Name == "Volo.Abp.Account.HttpApi");
}
For more information, please visit ASP.NET Core Docs
According to @cdlian answer this code snippet:
public override void OnPreApplicationInitialization(ApplicationInitializationContext context)
{
var applicationPartManager = context.ServiceProvider.GetRequiredService<ApplicationPartManager>();
applicationPartManager.ApplicationParts.RemoveAll(s => s.Name == "Volo.Abp.Account.HttpApi");
applicationPartManager.ApplicationParts.RemoveAll(s => s.Name == "Volo.Abp.FeatureManagement.HttpApi");
applicationPartManager.ApplicationParts.RemoveAll(s => s.Name == "Volo.Abp.Identity.HttpApi");
applicationPartManager.ApplicationParts.RemoveAll(s => s.Name == "Volo.Abp.PermissionManagement.HttpApi");
applicationPartManager.ApplicationParts.RemoveAll(s => s.Name == "Volo.Abp.SettingsManagement.HttpApi");
}
hides everything except:
- AbpApiDefinition
- AbpApplicationConfiguration
- AbpTenant
- EmailSettings
Still confused about this.
Is there any update on this?
@AlexanderSysoev
By overriding the ApplyForControllers method of AbpServiceConvention, you can remove controllers you don't need.
[Dependency(ReplaceServices = true)]
[ExposeServices(typeof(IAbpServiceConvention))]
public class MyServiceConvention : AbpServiceConvention
{
public MyServiceConvention(IOptions<AbpAspNetCoreMvcOptions> options, IConventionalRouteBuilder conventionalRouteBuilder) : base(options, conventionalRouteBuilder)
{
}
protected override void ApplyForControllers(ApplicationModel application)
{
application.Controllers.RemoveAll(s => s.ControllerType == typeof(AbpApplicationConfigurationController));
base.ApplyForControllers(application);
}
}
With v5.2 there is a built-in option to hide ABP-related endpoints on Swagger UI.
Call the HideAbpEndpoints method in your Swagger configuration as follows:
services.AddAbpSwaggerGen(
options =>
{
//... other options
//Hides ABP Related endpoints on Swagger UI
options.HideAbpEndpoints();
}
)
After that, ABP's default endpoints will still be functional, but will be hidden on Swagger UI. https://docs.abp.io/en/abp/5.2/API/Swagger-Integration#hide-abp-endpoints-on-swagger-ui
private static void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration) { context.Services.AddAbpSwaggerGenWithOAuth( configuration["AuthServer:Authority"], new Dictionary<string, string> { {"...", "... API"} }, options => { options.SwaggerDoc("v1", new OpenApiInfo {Title = "... API", Version = "v1"}); //options.DocInclusionPredicate((docName, description) => true); // <- Comment out this line of code options.CustomSchemaIds(type => type.FullName); }); }try it out
this is very helpful
It is possible to force authentication on EmailSettings API