IIS site within virtual directory Swagger UI end point

前端 未结 5 369
我在风中等你
我在风中等你 2020-12-19 00:35

Swagger UI end point is not same as dev in staging ( excluding domain names)

IIS Configuration

public void Configure(IApplicationBuilder app         


        
相关标签:
5条回答
  • 2020-12-19 01:14

    The problem is more relevant to swagger than Environment variable. Swagger does support the virtual directory which then the configuration should look like below. Note that virtual directory doesn't affect the UI End point.

    app.UseSwagger(c =>
                {
                    //Change the path of the end point , should also update UI middle ware for this change                
                    c.RouteTemplate = "api-docs/{documentName}/swagger.json";
                });
     app.UseSwaggerUI(c =>
                {
                    //Include virtual directory if site is configured so
                    c.RoutePrefix = "api-docs";
                    c.SwaggerEndpoint("v1/swagger.json", "Api v1");
                });
    
    0 讨论(0)
  • 2020-12-19 01:16

    Unfortunately, None of them works for me.
    I have tried all of them.

    Working solution:

    app.UseSwagger(c => {
        c.RouteTemplate = "swagger/{documentName}/swagger.json";
    });
    
    app.UseSwaggerUI(c => {
        c.SwaggerEndpoint("v1/swagger.json", "My API V1");
    });
    
    0 讨论(0)
  • 2020-12-19 01:18

    Adding "../" works for websites hosted under virtual directory and without virtual directory

    app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("../swagger/v1/swagger.json", "TestService");
            });
    
    0 讨论(0)
  • 2020-12-19 01:20

    I changed this line in Swagger UI configure (Startup.cs):

    c.SwaggerEndpoint("/prueba/swagger/v1/swagger.json", "Swagger (....)");
    
    0 讨论(0)
  • 2020-12-19 01:21

    It took me some time to get it running so i want to share my solution here

        string vpath = s.GetValue<string>("VirtualPath") ?? string.Empty;
    
        if (string.IsNullOrWhiteSpace(vpath))
        {
            app.UseSwagger();
            app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", endpointName); });
        }
        else
        {
                app.UseSwagger(c =>
                {
                    //no virtual path in the roue template it is relative 
                    c.RouteTemplate = $"swagger/{{documentName}}/swagger.json";
                    //c.PreSerializeFilters.Add((swagger, request) => swagger.BasePath = $"/{vpath}");
                });
                app.UseSwaggerUI(options =>
                {
                    //options.RoutePrefix = vpath;
                    //gives the location of the gennerated json file to the UI
                    //start with / to create an absolute path from the base directory
                    options.SwaggerEndpoint($"/{vpath}/swagger/v1/swagger.json", endpointName);
                });
        }
    
    0 讨论(0)
提交回复
热议问题