简体   繁体   English

如何使用 NSwag 和 C# 指定“scheme”元素?

[英]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.

相关问题 C#如何使用NSwag处理多态模型 - C# How to handle polymorphic models using NSwag NSwag:你如何使用自定义值 Object 类型 C# -> Swagger -> ZD7EFA19FBE7D23D772FDZ客户端560FBE7D23D7724客户端? - NSwag: How do you Use Custom Value Object Types in C# -> Swagger -> C# client? 如何使用 C# 更改鼠标方案 - How can I change mouse scheme using C# 如何使用Selenium在C#中访问此元素? - How do I access this element in C# using Selenium? 如何使用C#访问特定的HTML元素? - How do I access a specific HTML element using C#? 使用 NSwag for C# 我们如何在同一个客户端项目中为不同的 API 创建多个客户端类 - With NSwag for C# how do we create multiple client classes for different API in the same client project 如何在 C# / VSTO 中为 Excel 数据 Model 连接指定使用 XlCmdType.xlCmdTableCollection 的表 - How do I specify the tables using XlCmdType.xlCmdTableCollection for an Excel Data Model connection in C# / VSTO 如何在C#中为泛型类型指定多个约束? - How do I specify multiple constraints on a generic type in C#? c#如何在select语句中指定一个not? - c# How do I specify a not in a select statement? WorkflowDesigner - 如何指定表达式应该在C#中? - WorkflowDesigner - How do I specify expressions should be in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM