基于模块的多个 API 端点 [英] Multiple API endpoints based on Modules

查看:20
本文介绍了基于模块的多个 API 端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 ASP.NET Boilerplate v2.0.1 与 .NET Core 和 Angular 2 项目模板一起使用.

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

我的项目中有多个模块当前使用相同的 API 端点提供服务.请注意,将来我将为单个模块提供单独的 API 端点.

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.

由于每个模块的 API 端点都相同,因此通过 NSwag 生成的服务代理将拥有所有模块的服务.

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

我需要基于单个模块生成代理,因此,我想到了使用 API 版本控制来完成.因此,我将附加模块名称而不是 v1、v2 等.

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.

我有以下代码:

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.

推荐答案

看起来您正在使用 swashbuckle 而不是 NSwag 来生成您的 swagger 文档.有多种方法可以按版本分隔文档,请参阅 swashbuckle 文档 https://github.com/domaindrivendev/Swashbuckle.AspNetCore.默认方法是使用您上面的启动配置,并使用相应的 ApiExplorer 组名称装饰您的方法.组名需要与 swaggerdoc 配置中指定的第一个参数相匹配.

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

您还缺少第二个 swagger 端点的模块中的e".

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

参见示例:github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/4bbf5cacd6ad0817b9015d699c559fd5c1cedf0d/test/WebSites/MultipleVersions/Startup.cs

这篇关于基于模块的多个 API 端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆