【问题标题】:Can Swagger render the content of app.UseDeveloperExceptionPage()?Swagger 可以渲染 app.UseDeveloperExceptionPage() 的内容吗?
【发布时间】:2017-10-12 21:10:17
【问题描述】:

我有一个 ASP.NET Core 2.0 Web API,我通过 Swashbuckle 添加了 Swagger。哪个不错。

我还在 ASP.NET Core 中启用了app.UseDeveloperExceptionPage() 选项。哪个不错。

但他们在一起玩得不好。因为 SwaggerUI 只是将返回的 HTML 显示为文本。

对于 GET 请求,这不是什么大问题,因为我只是将 url 粘贴到浏览器中。但是对于 POST 不起作用。 所以我想知道当Content-Type 标头是text/html 时,是否有办法让SwaggerUI 呈现html。我一直找不到与此相关的任何内容,这有点令人费解:)

【问题讨论】:

    标签: asp.net-core swagger swagger-ui swashbuckle


    【解决方案1】:

    来自大摇大摆的开发者:

    我们必须清理 HTML 以避免注入标签和启用 XSS 漏洞。所以是的,原始 html 是您应该看到的。

    但是,you can make the change yourself to swagger-ui.js(代码由@nicksellen 友情提供):

    --- swagger-ui.js 2015-09-19 20:58:10.000000000 +0200
    +++ swagger-ui.js 2015-11-16 13:45:26.958266568 +0100
    @@ -31855,9 +31855,10 @@
    
         // HTML
         } else if (contentType === 'text/html') {
    -      code = $('<code />').html(_.escape(content));
    -      pre = $('<pre class="xml" />').append(code);
    -
    +      var iframe = document.createElement('iframe');
    +      iframe.srcdoc = content;
    +      iframe.style = 'width: 100%; height: 500px;';
    +      pre = iframe;
         // Plain Text
         } else if (/text\/plain/.test(contentType)) {
           code = $('<code />').text(content);
    

    使用 Swashbuckle 集成此更改:

    1. GitHub下载Swashbuckle
    2. swagger-ui.js 位于 bower_components\swagger-ui\dist 文件夹中的 Swashbuckle.AspNetCore.SwaggerUI 项目中。进行上述编辑并编译。
    3. 在您的项目中,删除 Swashbuckle NuGet 包并添加对在第 2 步中创建的 DLL 的引用。

    由于 NuGet 不再管理此包,因此您需要定期检查 Swashbuckle 项目并在需要更新时重复该过程。

    【讨论】:

    • 非常感谢。现在我只需要弄清楚如何将它与 Swashbuckle 集成 :)
    • 已编辑答案以包括如何与 Swashbuckle 集成。
    猜你喜欢
    • 2012-05-31
    • 2019-08-06
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 2017-11-09
    相关资源
    最近更新 更多