代码之家  ›  专栏  ›  技术社区  ›  Jay

虚拟目录内的IIS站点Swagger UI端点

  •  12
  • Jay  · 技术社区  · 8 年前

    Swagger UI端点与staging中的dev不同(不包括域名)

    enter image description here

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    
     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.SwaggerEndpoint(Configuration["Appsettings:VirtualDirectory"]+"api-docs/v1/swagger.json", "Api v1");                
            });
    
     services.AddSwaggerGen(c =>
            {
     var xmlDocPath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "Api.xml");
                c.IncludeXmlComments(xmlDocPath);
                c.DescribeAllEnumsAsStrings();
    

    使用上述配置

    发展

     "AppSettings": {
    "VirtualDirectory": "/"
    

    登台

     "AppSettings": {
    "VirtualDirectory": "/Api/"
    

    在登台打开的情况下,开发人员机器上UI的终点

    http://localhost:5001/api-docs/v1/swagger.json
    

    http://xxxx:5002/swagger/Api/api-docs/v1/swagger.json
    

    而不是(它应该是什么)

    http://xxxx:5002/Api/api-docs/v1/swagger.json
    
    6 回复  |  直到 8 年前
        1
  •  10
  •   Jay    8 年前

    与环境变量相比,这个问题与招摇更相关。Swagger确实支持虚拟目录,其配置如下所示。请注意,虚拟目录不会影响UI端点。

    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");
                });
    
        2
  •  8
  •   Gokulnath    5 年前

    添加“../”适用于托管在虚拟目录下且没有虚拟目录的网站

    app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("../swagger/v1/swagger.json", "TestService");
            });
    
        3
  •  1
  •   סטנלי גרונן Abhishek Singh    5 年前

    c.SwaggerEndpoint("/prueba/swagger/v1/swagger.json", "Swagger (....)");
    
        4
  •  1
  •   Tyler2P Suraj Mandal    5 年前

    不幸的是,它们都不适合我。
    我都试过了。

    工作解决方案:

    app.UseSwagger(c => {
        c.RouteTemplate = "swagger/{documentName}/swagger.json";
    });
    
    app.UseSwaggerUI(c => {
        c.SwaggerEndpoint("v1/swagger.json", "My API V1");
    });
    
        5
  •  1
  •   Darshani Jayasekara    4 年前

    对我有效的是,

                app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("swagger/v1/swagger.json", "MyDevOpsAPI V1");
            });
    

    注意,我删除了前导“/”。

        6
  •  0
  •   Manfred Wippel    7 年前

    我花了一些时间来运行它,所以我想在这里分享我的解决方案

        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);
                });
        }