- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以像 swashbuckle 那样拥有多个文档端点?
options.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1");
options.SwaggerEndpoint("/swagger/v2/swagger.json", "API v2");
如果是,如何修饰 api 调用,使一些属于一个版本,一些属于另一个版本?
所以根据 Rico Suter 的建议,我所做的有点像那样:
ApiVersionAttribute.cs
public class ApiVersionAttribute:Attribute
{
private List<string> _versions = new List<string>();
public List<string> Versions { get { return _versions; } }
public ApiVersionAttribute(string version) {
Versions.Add(version);
}
}
MyApiVersionProcessor.cs
public string Version { get; set; }
public MyApiVersionProcessor(string version)
{
this.Version = version;
}
public new Task<bool> ProcessAsync(OperationProcessorContext context)
{
bool returnValue = true;
var versionAttributes = context.MethodInfo.GetCustomAttributes()
.Concat(context.MethodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes())
.Where(a => a.GetType()
.IsAssignableTo("MapToApiVersionAttribute", TypeNameStyle.Name)
|| a.GetType()
.IsAssignableTo("ApiVersionAttribute", TypeNameStyle.Name)
)
.Select(a => (dynamic)a)
.ToArray();
var versionAttribute = versionAttributes.FirstOrDefault();
if (null == versionAttribute)
{
returnValue = false;
}
else
{
if (ObjectExtensions.HasProperty(versionAttribute, "Versions")
&& Version.Equals(versionAttribute.Versions[0].ToString()))
{
ReplaceApiVersionInPath(context.OperationDescription, versionAttribute.Versions);
}
else {
returnValue = false;
}
}
return Task.FromResult(returnValue);
}
private void ReplaceApiVersionInPath(SwaggerOperationDescription operationDescription,
dynamic versions)
{
operationDescription.Path = operationDescription.Path.Replace("{version:apiVersion}",
versions[0].ToString());
}
}
在我的 Global.asax 中
// NSwag
// https://github.com/RSuter/NSwag/wiki/OwinGlobalAsax#integration
app.UseSwaggerUi(typeof(WebApiApplication).Assembly, new SwaggerUiSettings
{
//TypeNameGenerator = new MyTypeNameGenerator(),
MiddlewareBasePath = "/swagger",
SwaggerRoute = "/swagger/v1/swagger.json",
Version = "1.0.0.0",
// https://github.com/RSuter/NSwag/wiki/Middlewares
OperationProcessors =
{
new MyApiVersionProcessor("v1")
},
PostProcess = document =>
{
document.BasePath = "/";
document.Produces
= new List<string> { "application/json"
, "text/json"
, "text/html"
, "plain/text"
, "application/xml"};
document.Consumes
= document.Produces;
document.Info.Title = "Document V1";
}
});
app.UseSwaggerUi(typeof(WebApiApplication).Assembly, new SwaggerUiSettings
{
//TypeNameGenerator = new MyTypeNameGenerator(),
MiddlewareBasePath = "/swagger",
SwaggerRoute = "/swagger/v2/swagger.json",
Version = "2.0.0.0",
OperationProcessors =
{
new MyApiVersionProcessor("v2")
},
PostProcess = document =>
{
document.BasePath = "/";
document.Produces
= new List<string> { "application/json"
, "text/json"
, "text/html"};
document.Consumes
= document.Produces;
document.Info.Title = "Document V2";
}
});
并用
装饰我的 Controller 方法[ApiVersion("v2")]
[ApiVersion("v1")]
等等
最佳答案
您可以定义 app.UseSwagger
两次并实现一个自定义操作处理器,该处理器根据您的需要仅过滤所需的 api 操作(即在第一次调用中您应该过滤所有版本 x,在第二次调用中过滤所有y 的版本)。
目前默认添加的ApiVersionProcessor
只会将路由路径中的版本占位符替换为第一个声明的版本。我认为我们应该扩展此处理器,以便您可以排除版本并插入正确的版本。
顺便说一句:我是 NSwag 的作者。
关于NSwag 多文档端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45238908/
我正在尝试从 .yaml 文件生成 .NET Core Web API Controller 。 有一个运行此命令的示例: dotnet "/.../dotnet-nswag.dll" openapi
我正在尝试从 .yaml 文件生成 .NET Core Web API Controller 。 有一个运行此命令的示例: dotnet "/.../dotnet-nswag.dll" openapi
我想对源自程序集的所有 API 调用进行分组,以便一起显示在 Swagger UI 上,但看到分组默认发生在 Controller 上。有什么方法可以强制分组处于装配级别?我正在使用 NSwag 生成
我正在使用 NSwag SwaggerToCSharpClientGenerator 生成请求和响应类以及枚举。 我有一些 Web API 方法共享相同的枚举作为参数或作为请求对象的一部分。当我运行
是否可以像 swashbuckle 那样拥有多个文档端点? options.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1"); options
是否可以生成客户端代码,以便模型的类名具有完整的命名空间作为前缀? 这应该避免相同的类名冲突。 例子 com.foo.MyClass 和 it.foo.MyClass 到现在我得到的是MyClass和
我正在使用 NSwag 生成我的 TypeScript 客户端(Angular 格式),但由于循环依赖关系,我在实现 Bearer token 身份验证时遇到了问题。 我的用户服务需要导入 TS 客户
将 VS 连接服务 (NSwag) 注入(inject)到类/ Controller 中的首选方法是什么。我在网上找到了很多使用这种表格的建议: services.AddHttpClient((pro
我有一个项目,它使用 NSwag 从 swagger 文件生成客户端和契约(Contract)。我不希望 git 跟踪这些生成的文件,因此当项目在构建服务器上构建时,它会将它们作为构建的一部分生成。
我将 NSwag 用于 .NET Core 3.1。一切正常。 我无法确定如何将“我的标题”(即信息标题)更改为其他内容。 这是招摇页面: 这是我的注册码: app.UseOpenApi(); app
我从供应商处获得了 (OpenApi 3.0.1) 中的对象规范: "ExampleTO" : { "codeValidFrom" : { "type" : "string", "
我有一个多态模型: public class CreateOrderRequest { public List OrderItems { get; set; } } /// /// Iden
我在一个 ASP.Net WebAPI 项目中使用 NSwag 来生成一个 swagger 接口(interface)——效果很好。 假设我有一个方法我想添加一些解释 - 我该怎么做? 评论是指 AP
我有这样设置的自定义授权方案: services.AddAuthentication("ClientApp") .AddScheme("ClientApp", null
我有一个类: class Test { public IPAddress Ip {get; set; } } 默认情况下,Nswag(Nswag.AspNetCore 包)位于 http://lo
我使用 NSwag 为单个 Controller 生成客户端;我需要它作为自己独立的客户端。我希望将来生成 Swagger 规范时忽略它。 我试着在 Controller 的顶部添加这个属性,但它仍然
我有一个如下所示的 API 方法: [HttpGet("top/{max}", Name = "GetTopLocations")] public ActionResult> GetTop(int m
在我的项目中无法使用 JsonIgnore,并且 [OpenApiIgnore] 不起作用。在 swashbuckle 中可以通过它自己的属性来制作过滤器,但是在 NSwag 中我没有找到类似的机制。
我们正在使用 swagger/nswag 来记录 webapi 项目。 作为 ActionMethods 的 BodyParameters,我们使用带有后缀 Command 的类,其中包含例如参数。创
当我使用 NSwag 为 API 生成 C# 客户端时,其中 API 包含可用于多种 Http 请求类型(例如 POST、GET)的端点客户端为每个具有相同基本名称的请求生成一个方法,外加一个数字。
我是一名优秀的程序员,十分优秀!