简体   繁体   English

Swashbuckler / Swagger忽略方案设置

[英]Swashbuckler/Swagger ignores Scheme setting

I'm using Swashbuckler ver. 我正在使用Swashbuckler ver。 5.6.0 in webapi project configuration says: c.Schemes(new[] { "https" }); 在webapi项目中5.6.0配置说:c.Schemes(new [] {“ https”});

Despite of this, when I access the swagger documentation page for my site, it tries to load: http://{swagger-docs-url}/swagger/v1, instead of https, and fails because of mixed content 尽管如此,当我访问网站的swagger文档页面时,它会尝试加载:http:// {swagger-docs-url} / swagger / v1,而不是https,并且由于内容混合而失败

What am I doing wrong here? 我在这里做错了什么? Can th 可以

The c.Schemes(new[] { "https" }); c.Schemes(new[] { "https" }); in the configuration only impacts the generated swagger: 在配置中仅影响生成的摇摇欲坠:

That will force the following output: 这将强制执行以下输出:

 {
   "swagger": "2.0",
   "info": {
     "version": "V1",
     "title": "Swagger_Test",
   },
   "host": "yourhost.azurewebsites.net",
   "schemes": [
     "https"
   ],

Your question is not complete but I believe that what you are looking for is the rootUrl: https://github.com/domaindrivendev/Swashbuckle#rooturl 您的问题尚不完整,但我相信您要寻找的是rootUrl: https : //github.com/domaindrivendev/Swashbuckle#rooturl

Actually, Swashbuckle will detect Uri on host and use the Uri Scheme / port as your {swagger-docs-url} , so if your host running on HTTP it only got HTTP, you can custom the rooturl to fix it. 实际上,Swashbuckle会在主机上检测到Uri并将Uri方案/端口用作您的{swagger-docs-url} ,因此,如果在HTTP上运行的主机只有HTTP,则可以自定义rooturl对其进行修复。

//SwaggerConfig.cs
config.EnableSwagger(c =>
{
    c.RootUrl(ResolveBasePath);
    c.Schemes(new[] { "http", "https" });
}

internal static string ResolveBasePath(HttpRequestMessage message)
{
    //fix for Cloudflare Flexible SSL and localhost test
    var scheme = message.RequestUri.Host.IndexOf("localhost") > -1 ? "http://" : "https://";
    return scheme + message.RequestUri.Authority;
}

I have a site using Cloudflare Flexible SSL, server running on HTTP but inbound connection to Cloudflare was HTTPS, so I need to force the Uri scheme as HTTPS, unless I test on my localhost. 我有一个使用Cloudflare Flexible SSL的站点,该服务器运行在HTTP上,但与Cloudflare的入站连接是HTTPS,因此除非我在本地主机上进行测试,否则我必须将Uri方案强制为HTTPS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM