I have API Versionning swagger
namespace AccountingService
{
public class Startup
{
//public Startup(IConfiguration configuration, IHostEnvironment env, ILogger<Startup> logger) { }
//public void ConfigureServices(IServiceCollection services) { }
public void Configure(IApplicationBuilder app, IServiceProvider serviceProvider, IApiVersionDescriptionProvider provider)
{
if (HostEnvironment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
UserHelper.InitDependencies(serviceProvider.GetRequiredService<IUserDataService>());
app.ConfigureCustomMiddleware(Configuration, _appSettings);
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseUserService();
if (_appSettings.Swagger.Enabled)
{
app.UseSwagger();
app.UseSwaggerUI(options =>
{
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToLowerInvariant());
options.RoutePrefix = $"{_appSettings.ServiceName}/swagger";
}
});
}
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
And there are a bunch of controllers with routing where the attributes of the controller are written like this
[ApiVersion("1.0")]
[Route("/api/v{version:apiVersion}/[controller]")]
[ApiController]
public class AccountCardController : CommonController
{
//CRUD methods
//BLL methods
//Utils methods
}
And everything seems to be fine, but I need to add the name of the service itself to the routing of each methods There is a solution for this, I have to register it manually for each controller, but this is wrong (I thought) and nonsense
Api route expect Like that add accounting-service everywhere at first and it will turn out like this
accounting-service/api/v{version:apiVersion}/[controller]
But I know for sure that this can be done somehow through the settings in the startup file, I don’t know the swagger settings so deeply to find them, in google they say to do it through the endpoints.MapControllerRoute() method, but this is something not exactly what i need, it doesn’t help me
CodePudding user response:
Swagger can't help. It produces a documentation of the available interfaces, but can't change the routing. While you could change the documentation through swagger by writing a SchemaProcessor
or a DocumentProcessor
, this would produce invalid URLs, that couldn't be processed by ASP core.
To solve this issue, you have to change your RouteAttribute
on each controller class and apply the desired value.
[ApiVersion("1.0")]
// add desired value into route
[Route("accounting-service/api/v{version:apiVersion}/[controller]")]
[ApiController]
public class AccountCardController : CommonController
{
//CRUD methods
//BLL methods
//Utils methods
}
This is the cleanest approach by the given possibilities from ASP.core. Even if you have dozens of controllers that need to be changed, it will take you less then an hour to do so and if you would have to change hundreds of controllers you could think about a regular expression to run within "Find & Replace" to update all places.
Another approach would be to write your own middleware, that re-writes the incoming request before it is forwarded to ASP routing, but IMHO this is overkill in this case.