【问题标题】:Publishing web api with swagger on IIS在 IIS 上大张旗鼓地发布 web api
【发布时间】:2020-07-23 16:07:31
【问题描述】:

在遵循此示例 documentation 之后,我正在尝试弄清楚如何使用 Swagger (SwashBuckle) 发布 .net core 3 API。所以它在本地工作,当我点击 F5 IIS Express 时,会在 http://localhost:8033/index.html 下启动该站点

这是 startup.cs 中的配置代码:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(env.ContentRootPath),
                RequestPath = new PathString("")
            });

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
                c.DocumentTitle = "TestAPI";
                c.DocExpansion(DocExpansion.None);
                c.RoutePrefix = string.Empty;                   
            });                
        }

接下来,我将 API 发布到本地文件夹,并将文件复制到服务器上的 IIS 文件夹中。如果我打开服务器 API 域,我会得到一个 page can’t be found。我应该使用哪个地址来打开服务器上的 swagger UI?配置中是否缺少某些内容?

【问题讨论】:

标签: c# asp.net-core iis swagger swashbuckle


【解决方案1】:

您的Swagger 设置没有问题。请不要忘记配置Swagger 生成器,以及Swagger JSON 的cmets 路径。

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "ToDo API",
                    Description = "A simple example ASP.NET Core Web API",
                    TermsOfService = new Uri("https://example.com/terms"),
                    Contact = new OpenApiContact
                    {
                        Name = "Shayne Boyer",
                        Email = string.Empty,
                        Url = new Uri("https://twitter.com/spboyer"),
                    },
                    License = new OpenApiLicense
                    {
                        Name = "Use under LICX",
                        Url = new Uri("https://example.com/license"),
                    }
                });
                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });
        }

另外,请确保服务器已在服务器端安装了 Asp.net 核心托管包。
https://dotnet.microsoft.com/download/dotnet-core/thank-you/runtime-aspnetcore-3.1.6-windows-hosting-bundle-installer
如果有什么可以帮助的,请随时告诉我。

【讨论】:

  • 嗨@AbrahamQian 感谢您的解释。我已经准备好所有代码,并且 API 正在工作,我能够从客户端触发 REST 调用。但是 SwaggerUI 并未显示在服务器上。我认为像 MyServerAPI/index.html 这样指向 index.html 就足够了?即使项目中没有静态文件或 wwwroot 文件夹,我认为 Swagger 会即时生成它?
  • 是的,该 URL 足以导航 SwaggerUI。但我不确定第二个问题。您可以尝试在 CSPROJ 文件中配置,让编译器生成文件,就像文档说的那样。
【解决方案2】:

补充一下,也可能是 PropertyGroup 节点下的 API 项目文件中缺少以下节点。

<RuntimeIdentifiers>win10-x64;</RuntimeIdentifiers>
<AspNetCoreModuleName>AspNetCoreModuleV2</AspNetCoreModuleName>

【讨论】:

    【解决方案3】:

    编辑您的 Startup.cs 文件

     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
    
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
    
            if (env.IsProduction() || env.IsStaging())
            {
                app.UseExceptionHandler("/Error/index.html");
            }
    
            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger(c =>
            {
                c.RouteTemplate = "swagger/{documentName}/swagger.json";
            });
    
            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.RoutePrefix = "swagger";
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1");
    
                // custom CSS
                c.InjectStylesheet("/swagger-ui/custom.css");
            });
    
            app.Use(async (ctx, next) =>
            {
                await next();
                if (ctx.Response.StatusCode == 204)
                {
                    ctx.Response.ContentLength = 0;
                }
            });
    
    
            app.UseHttpsRedirection();
    
            app.UseRouting();
    
            app.UseAuthorization();
            app.UseAuthentication();
    
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
    
            app.UseCors();
    
        }   
        
      
    
      
    

    https://youtu.be/6HiaXCAlRr0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 2018-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多