1
votes

I am using ASP.NET Boilerplate v2.0.1 with .NET Core and Angular 2 project template.

I have multiple modules in my project that are currently served using same API endpoint. Please note, in the future I will have separate API endpoint for an individual module.

As the API endpoint is same for every module, the service proxy which is generated through NSwag will have all the modules' services.

I am having a requirement to have proxy generated based on an individual module, so for this, I thought of accomplishing using API versioning. So instead of v1, v2 etc., I will append the module name.

I have the following code:

Configuration.Modules.AbpAspNetCore()
             .CreateControllersForAppServices(
                 typeof(Module).GetAssembly(),
                 "modulename"
             );

services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("module1", new Info { Title = "Module 1", Version = "module1" });
    options.SwaggerDoc("module2", new Info { Title = "Module 2", Version = "module2" });
}

app.UseSwagger(options =>
{
    options.RouteTemplate = "swagger/{documentName}/swagger.json";
});

// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
app.UseSwaggerUI(options =>
{
    options.SwaggerEndpoint("/swagger/module1/swagger.json", "Module 1");
    options.SwaggerEndpoint("/swagger/module2/swagger.json", "Module 2");
});

This way, I am able to generate two endpoints as follows:

http://localhost:5000/swagger/module1/swagger.json

http://localhost:5000/swagger/module2/swagger.json

But both are having all the modules' information.

Please correct me or suggest a way to achieve the functionality.

2

2 Answers

1
votes

It looks like you are using swashbuckle and not NSwag to generate your swagger docs. There are multiple ways to separate your docs by version, see swashbuckle documentation https://github.com/domaindrivendev/Swashbuckle.AspNetCore. The default method is to use your startup configs that you have above and decorate your methods with the corresponding ApiExplorer group name. The group name needs to match the first argument specified in the swaggerdoc config.

[ApiExplorerSettings(GroupName = "module1")] //Module 1 Method

You are also missing an 'e' in module for the second swagger endpoint.

see example: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/4bbf5cacd6ad0817b9015d699c559fd5c1cedf0d/test/WebSites/MultipleVersions/Startup.cs

0
votes

To add multiple source edit the SwaggerUIOptions. For example:

app.UseSwaggerUI(c => 
{
    c.SwaggerEndpoint("http://<ip1>:<port>/swagger/v1/swagger.json", "ServiceName1");
    c.SwaggerEndpoint("http://<ip2>:<port>/swagger/v1/swagger.json", "ServiceName2");
});