【问题标题】:Issue Using Custom Index.Html in Swagger / Swashbuckle for .NET Core在 Swagger / Swashbuckle for .NET Core 中使用自定义 Index.Html 的问题
【发布时间】:2018-03-01 14:42:32
【问题描述】:

我在使用自定义 index.html 和其他带有 swashbuckle 的资产时遇到了困难。 Swashbuckle/Swagger 似乎根本不认识或使用它们。我确实有 app.UseDefaultFiles() 和 app.UseStaticFiles() 设置。我试图了解我做错了什么。

我试图设置我的配置,有点类似于 Microsoft 文章中定义的配置,但没有成功。 (https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs=visual-studio)

我目前正在使用文章 (https://github.com/swagger-api/swagger-ui/tree/2.x/dist) 中引用的 dist 文件夹中的文件以及提供的自定义 css 文件。

我的 index.html 文件位于 /wwwroot/swagger/ui 下 自定义 css 文件位于 /wwwroot/swagger/ui/css 下(作为 custom.css)

这是我的 Startup.cs 类。

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc()
             .AddJsonOptions(options =>
             {
                 // Swagger - Format JSON
                 options.SerializerSettings.Formatting = Formatting.Indented;
             });

        // Register the Swagger generator, defining one or more Swagger documents
        services.AddSwaggerGen(c =>
        {
            c.DescribeAllEnumsAsStrings();
            c.DescribeStringEnumsInCamelCase();
            // c.DescribeAllParametersInCamelCase();                

            c.SwaggerDoc("v1",
                new Info
                {
                    Title = "My Web API - v1",
                    Version = "v1",
                    Description = "New and improved version. A simple example ASP.NET Core Web API. "

                }
            );

            c.SwaggerDoc("v2",
                new Info
                {
                    Title = "My Web API - v2",
                    Version = "v2",
                    Description = "New and improved version. A simple example ASP.NET Core Web API. "
                }
            );

            // Set the comments path for the Swagger JSON and UI.
            var basePath = AppContext.BaseDirectory;
            var xmlPath = Path.Combine(basePath, "ApiTest.xml");
            c.IncludeXmlComments(xmlPath);
        });

    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        string swaggerUIFilesPath = env.WebRootPath + "\\swagger\\ui";

        if (!string.IsNullOrEmpty(swaggerUIFilesPath))
        {
            app.UseDefaultFiles();
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(swaggerUIFilesPath),
                RequestPath = new PathString("/api-docs"),
            });
        }

        // Enable middleware to serve generated Swagger as a JSON endpoint.
        app.UseSwagger(c =>
        {
            c.RouteTemplate = "api-docs/{documentName}/swagger.json";
        });

        // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
        app.UseSwaggerUI(c =>
        {
            //c.ShowJsonEditor();
            c.RoutePrefix = "api-docs";
            c.SwaggerEndpoint("/api-docs/v1/swagger.json", "My Web API - V1 ");
            c.SwaggerEndpoint("/api-docs/v2/swagger.json", "My Web API - V2 ");
            c.DocumentTitle("My Web API");
        });

        app.UseMvc();
    }
}

我的最终目标是能够使用类似于此处可用的石板风格主题 (https://github.com/omnifone/slate-swagger-ui)。目前,我只是想让 Swashbuckle/Swagger 使用 Microsoft 文档中引用的自定义文件,然后再尝试使其他文件正常工作。

我真的不想尝试将我的资产转换为嵌入式资源——因为其中有很多。我只想引用一个普通的 index.html 文件并能够使用它所有引用的文件。

我做错了什么?

相关软件版本

  • .Net Core 版本:2.0.3
  • Swashbuckle.AspNetCore:1.2.0
  • Windows 10 企业版 1703
  • Visual Studio 2017 企业版 15.5.2

【问题讨论】:

  • 请忽略上述问题。没有问题。有时在向 Stack Overflow 提交问题之前,在 Chrome 中按 Ctrl-F5 以清除缓存很有用。对于那些可能需要在 ASP.NET Core 2.x 中使用带有 Swashbuckle 的 Index.Html 的人,上面的代码 sn-p 应该可以正常工作。

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


【解决方案1】:

这是我发现在 .NET Core 项目中替换 SwashBuckle 的 index.html 所需的最少操作:

  • 从此处获取原始 index.html 的副本:https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/src/Swashbuckle.AspNetCore.SwaggerUI/index.html

  • 将该副本放在项目的某个子文件夹中。
    该文件可能有不同的名称,我选择: \Resources\Swagger_Custom_index.html

  • 在解决方案资源管理器中右键单击该文件,选择“属性”,在左侧窗格中选择“配置属性”。在右窗格的“高级”下找到条目“构建操作”并将其设置为“嵌入式资源”。点击确定。

  • 在 Startup.cs 中将以下行添加到您的 app.UseSwaggerUI() 调用中:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        //...
    
        app.UseSwaggerUI(c =>
        {
            c.IndexStream = () => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("Your.Default.Namespace.Resources.Swagger_Custom_index.html");
        });
    
        //...
    }
    
  • 上述GetManifestResourceStream方法中文件资源的标识符由以下几部分组成:

    1. 您的默认命名空间(即“Your.Default.Namespace”)
    2. 资源的子路径(即“资源”)
    3. 资源的文件名(即“Swagger_Custom_index.html”)

    所有三个部分都使用点连接(此处没有斜杠或反斜杠)。
    如果您不使用子路径但将资源放在根目录中,则省略第 2 部分。

【讨论】:

  • 这段代码如何在static 方法中工作?我的配置是一个静态方法。
  • 我完全按照您的描述进行了尝试,但是如何在浏览器中访问 HTML 文件?我应该包括什么路径?我的 HTML 文件位于根目录的 Resources 文件夹中。
  • @aakash 你试过通过 /swagger 的标准访问吗?
  • 它加载默认的招摇用户界面,不加载Swagger_Custom_index.html。当我尝试https://localhost:44397/swagger 它重定向到https://localhost:44397/swagger/index.html
【解决方案2】:

对于在 ASP.NET Core 上分离 ApplicationBuilder 配置方法的人:

如果分离的方法/类是静态的,则无法调用GetType(),因为需要对象引用。

在这种情况下,将GetType() 切换为MethodBase.GetCurrentMethod().DeclaringType

c.IndexStream = () => MethodBase.GetCurrentMethod().DeclaringType.Assembly.GetManifestResourceStream("xxx.index.html");

【讨论】:

    猜你喜欢
    • 2021-03-01
    • 2018-07-29
    • 1970-01-01
    • 2020-12-13
    • 2018-10-22
    • 2018-10-16
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多