首页 文章

问题在Swagger / Swashbuckle for .NET Core中使用自定义Index.Html

提问于
浏览
1

我很难使用自定义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核心版本:2.0.3

  • Swashbuckle.AspNetCore:1.2.0

  • Windows 10 Enterprise Build 1703

  • Visual Studio 2017 Enterprise 15.5.2

1 回答

  • 3

    以下是我发现在.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 方法中文件资源的标识符由以下内容组成:

    • 您的默认命名空间(即'Your.Default.Namespace')

    • 资源的子路径(即'Resources')

    • 资源的文件名(即'Swagger_Custom_index.html')

    所有三个部分都使用点连接(这里没有斜线或反斜杠) .
    如果您不使用子路径但将资源放在root中,则只需省略第2部分 .

相关问题