Swagger 可以渲染 app.UseDeveloperExceptionPage() 的内容吗?

Can Swagger render the content of app.UseDeveloperExceptionPage()?

我有一个 ASP.NET Core 2.0 Web API,我在其中通过 Swashbuckle 添加了 Swagger。哪个好。

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

但他们相处得并不融洽。因为 SwaggerUI 只是将返回的 HTML 显示为文本。

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

来自 swagger 开发者:

We have to clean the HTML to avoid injecting tags and enabling XSS vulnerabilities. So yes, raw html is what you should expect to see.

但是,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
  2. 下载 Swashbuckle
  3. swagger-ui.js在bower_components\swagger-ui\dist文件夹下的Swashbuckle.AspNetCore.SwaggerUI项目中。进行上述编辑并编译。
  4. 在您的项目中,删除 Swashbuckle NuGet 包并添加对在步骤 2 中创建的 DLL 的引用。

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