首页 > 解决方案 > Swashbuckle Swagger url 从 http 到 https .NET

问题描述

我的 API 是在 ASP.NET 中开发的 HTTP API(不是 HTTPS)。

然而,当部署在 DMZ 中时,在网络上的某个地方,不属于我的职责范围内的某些东西只接受 HTTPS 并充当我的 HTTP API 的反向代理。

这导致了招摇的 UI 失礼。

访问 swagger UI 的 URL 是“https://external_public_name/api/swagger/ui/index#/”,但他在绿丝带的文本字段中的一个是“http://external_public_name/api/swagger/docs/ v1”。

因此,页面为空白,控制台出现错误

混合内容:“https://external_public_name/api/swagger/ui/index”页面通过 HTTPS 加载,但请求了不安全的 XMLHttpRequest 端点“http://external_public_name/api/swagger/docs/v1”。此请求已被阻止;内容必须通过 HTTPS 提供。

用 HTTPS 手动替换文本框中的 URL 工作正常。

我应该在哪里更改某些内容以更新该 URL(可能在运行时)?

PS:我已经不得不将 SwaggerConfig.cs 重写为 SwaggerConfig.vb 才能使用它。

Imports System.IO
Imports System.Reflection
Imports System.Web.Http
Imports System.Web.Http.Description
Imports Swashbuckle.Application
Imports Swashbuckle.Swagger

<Assembly: PreApplicationStartMethod(GetType(SwaggerConfig), "Register")>

Public Class SwaggerConfig
    Public Class KnownSubTypesDocumentFilter
        Implements IDocumentFilter

        Public Sub Apply(swaggerDoc As SwaggerDocument, schemaRegistry As SchemaRegistry, apiExplorer As IApiExplorer) Implements IDocumentFilter.Apply
            schemaRegistry.GetOrRegister(GetType(BookController.BookList))
        End Sub
    End Class

    Public Shared Sub Register()
        Dim thisAssembly = GetType(SwaggerConfig).Assembly

        GlobalConfiguration.Configuration.EnableSwagger(
            Function(c)
                c.Schemes({"https"})
                c.SingleApiVersion("v1", "api")
                c.DocumentFilter(Of KnownSubTypesDocumentFilter)()
                Return c
            End Function).EnableSwaggerUi(
            Function(ui)
                ui.DocumentTitle("Skippy's API")
                ui.DocExpansion(DocExpansion.List)
                ui.SupportedSubmitMethods()
                Return ui
            End Function)

    End Sub
End Class

标签: asp.net-mvcswaggerswagger-uiswashbuckle

解决方案


推荐阅读