[英]How do I specify the "scheme" element using NSwag and C#?
I'm using ASP.NET Core and NSwag to host and describe a new web service hosted in IIS with Windows Authentication.我正在使用 ASP.NET Core 和 NSwag 来托管和描述一个新的 web 服务,托管在 IIS 中,使用 Windows 身份验证。
Locally I run the web service using https, but when I deploy to a test environment the web service sits behind a load balancer with SSL-offloading.我在本地使用 https 运行 web 服务,但是当我部署到测试环境时,web 服务位于具有 SSL 卸载功能的负载平衡器后面。 This means that even though the site appears to run under SSL in the browser, the actual binding in IIS is http. So my Swagger UI page (and swagger.json definition) describes the schemes supported as http.
这意味着即使该站点在浏览器中显示为在 SSL 下运行,但在 IIS 中的实际绑定是 http。因此我的 Swagger UI 页面(和 swagger.json 定义)将 8274 支持的 schemes4 描述为 18.
I'd like the Schemes element in the Swagger.json that I use to read "https" instead of "http".我想要 Swagger.json 中的 Schemes 元素,我用它来读取“https”而不是“http”。 Would anyone be able to help me find the property I need to set in my code to set the scheme manually?
谁能帮我找到我需要在我的代码中设置的属性来手动设置方案?
{
x-generator: "NSwag v11.19.1.0 (NJsonSchema v9.10.72.0 (Newtonsoft.Json v11.0.0.0))",
swagger: "2.0",
info: {
title: "My API title",
description: "Provides access to data.",
version: "1.0.0"
},
host: "myhostname.net",
schemes: [
"http"
],
etc...
}
Boom. 繁荣。 Got it!
得到它了!
Finally found an answer on Github and the following code did the trick: 终于在Github上找到了答案,下面的代码可以解决问题:
app.UseSwaggerWithApiExplorer(config =>
{
//...other code omitted...
config.PostProcess = settings =>
{
settings.Schemes.Clear();
settings.Schemes.Add(NSwag.SwaggerSchema.Https);
};
});
EDIT: 编辑:
for NSwag v12
use: 对于
NSwag v12
使用:
app.UseSwagger(configure => configure.PostProcess = (document, _) => document.Schemes = new[] { SwaggerSchema.Https });
My project was using NSwag v13 and the below worked for me.我的项目使用的是 NSwag v13,下面的项目对我有用。
app.UseOpenApi(a => {
a.PostProcess = (document, _) => {
document.Schemes = new[] { OpenApiSchema.Https, OpenApiSchema.Http };
};
});
Source: https://github.com/RicoSuter/NSwag/issues/1545来源: https://github.com/RicoSuter/NSwag/issues/1545
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.