在.net核心中修复了以下Api版本控制之后
services.AddApiVersioning(options =>
{
options.AssumeDefaultVersionWhenUnspecified = true;
options.DefaultApiVersion = new ApiVersion(1, 0);
options.UseApiBehavior = false; //If true [ApiController] attribute required for versioning
})
.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
我面临的下一个问题是让默认的api版本显示swagger文档。如下图所示。
有很多文章使用swashbuckle来修复它,但是如何使用NSwag来修复它呢?
发布于 2021-03-25 00:56:54
有了NSwug,我们可以使用DocumentProcessors来过滤掉不想要的swagger路径。
我已经使用了下面的nuget包
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" />
<PackageReference Include="NSwag.AspNetCore" Version="13.9.4" />
下面显示了我是如何注册它们并修复问题的
public void ConfigureServices(IServiceCollection services)
{
....
services.AddApiVersioning(options =>
{
options.AssumeDefaultVersionWhenUnspecified = true;
options.DefaultApiVersion = new ApiVersion(1, 0);
})
.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
services.AddSwaggerDocument(config =>
{
SwaggerConfigure(config, "v1", true);
});
services.AddSwaggerDocument(config =>
{
SwaggerConfigure(config, "v2", false);
});
}
SwaggerConfigure实现
private void SwaggerConfigure(AspNetCoreOpenApiDocumentGeneratorSettings configure, string version, bool isDefaultVersion)
{
configure.DocumentName = version;
configure.ApiGroupNames = new[] { version };
if (isDefaultVersion)
{
configure.DocumentProcessors.Add(new RemoveVersionFromDefault(version));
}
configure.PostProcess = document =>
{
document.Info.Version = version;
document.Info.Title = $"{GetApiTitle()} API";
};
configure.AllowNullableBodyParameters = false;
configure.SerializerSettings = new JsonSerializerSettings();
}
DocumentProcessor实现
public class RemoveVersionFromDefault : IDocumentProcessor
{
private readonly string _defaultVersion;
public RemoveVersionFromDefault(string defaultVersion)
{
_defaultVersion = defaultVersion;
}
public void Process(DocumentProcessorContext context)
{
var keys = new List<string>();
foreach (var (key, value) in context.Document.Paths)
{
if (key.Contains($"/{_defaultVersion}/"))
{
keys.Add(key);
}
}
foreach (var key in keys)
{
context.Document.Paths.Remove(key);
}
}
}
就是这样。我希望这能帮助那些正在寻找类似的NSwug解决方案的人
发布于 2021-03-25 03:47:49
有几种方法可以解决这种情况。另一种方法是创建并注册最后执行的自定义IApiDescriptionProvider。从提供给IApiDescriptionProvider.OnProvidersExecuted
上下文的结果ApiDescription实例中,您可以找到您要查找的内容,并从结果中删除描述符。在此场景中,您希望删除与默认API版本和API版本参数匹配的描述符。
这种方法有几个好处:
通过注入IOptions<ApiVersioningOptions>
ApiDescriptor.GetApiVersion
扩展方法的版本和仍然带有ApiVersioningOptions.RouteConstraintName
中获取名称。不需要解析或匹配魔术字符串。旁白:关于
options.UseApiBehavior = false; //If true [ApiController] attribute required for versioning
的评论
不是很准确。你可能得到了你想要的结果,但故事中还有更多的东西。这种行为和定制没有很好的文档记录,几乎没有人对其进行更改。这种行为实际上是由IApiControllerSpecification和IApiControllerFilter控制的。默认的过滤器实现只运行所有已注册的规范,因此几乎没有理由对其进行更改。每个规范都定义了控制器是否匹配。有一个与[ApiController]
匹配的开箱即用的规范,但这并不是唯一存在的规范(例如: OData控制器)。之所以存在UseApiBehavior
选项,是因为在[ApiController]
之前(我认为是在2.1中),没有明确的方法来区分仅限UI的控制器和仅限API的控制器。这真的让一些混合了这两种控制器的开发人员感到恼火。相反,翻转硬开关来禁用可能会破坏想要该行为的开发人员。将值设置为false
只会忽略所有过滤,并恢复到[ApiController]
之前的日子,API是在一个名为API Behaviors的功能增强中引入的。如果您决定这样做,新设计允许您替换或创建新规范。它可能不适用于您,但应该知道,配置选项不是用于匹配[ApiController]
和其他更复杂的控制器过滤的硬on
或off
。
https://stackoverflow.com/questions/66785762
复制相似问题