【问题标题】:.Net Mvc Swashbuckle own Index.page not rendering properly no javascript no css .Net Mvc Web Api.Net Mvc Swashbuckle 自己的 Index.page 无法正确呈现没有 javascript 没有 css .Net Mvc Web Api
【发布时间】:2017-02-20 06:04:47
【问题描述】:

我在 .net mvc web api 的 swashbuckle 中创建了自己的索引页面。我从这个链接复制了源代码

https://github.com/domaindrivendev/Swashbuckle/blob/master/Swashbuckle.Core/SwaggerUi/CustomAssets/index.html

当我运行这个 web api 我得到这个输出

Screen Shot Output

我正在使用 Swashbuckle.Core 1.0.0.0 运行时版本 v4.0.30319

请帮帮我 我还有什么要放静态 CSS 和 JS 文件的吗?

【问题讨论】:

    标签: .net asp.net-mvc asp.net-web-api swashbuckle


    【解决方案1】:

    如果您正在自定义 index.html 页面,则可以通过更改 index.html 页面中的 addApiKeyAuthorization javascript 函数并在 SwaggerConfig.cs 类中添加以下属性来添加自己的标题。

    SwaggerConfig.cs

    config.EnableSwagger(
    c =>
    {
    c.ApiKey("apiKey")
    .Description("API Key Authentication")
    .Name("apiKey")
    .In("header");
    })
    
    function addApiKeyAuthorization() {
    var key = encodeURIComponent($('#input_apiKey')[0].value);
    if (key && key.trim() != "") {
    
    key = "Bearer " + key;
    
              var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization("Authorization", key, "header");
              window.swaggerUi.api.clientAuthorizations.add("key", apiKeyAuth);
              log("added key " + key);
          }
      }
    

    【讨论】:

    • 我之前看过这个解决方案,但是把函数放在哪里就像我制作了一个 .js 文件但 js 文件注入不起作用
    • 尝试在wwwroot文件夹中添加脚本和CSS文件,并在中间件中指定相对路径。
    【解决方案2】:

    我发现将包含我的自定义 index.html 的文件夹从“Swagger”重命名为其他名称可以解决此问题。

    GitHub issue 中查看 rbonstell 的帖子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-24
      • 2017-08-28
      • 2020-01-16
      • 2012-06-18
      • 1970-01-01
      • 1970-01-01
      • 2016-04-18
      • 1970-01-01
      相关资源
      最近更新 更多