在IIS上 swagger 地发布Web API

wfsdck30  于 2023-01-13  发布在  其他
关注(0)|答案(4)|浏览(215)

我正在尝试弄清楚如何在遵循这个示例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?配置中是否缺少一些内容?

xoshrz7s

xoshrz7s1#

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

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
如果有什么我能帮忙的尽管告诉我。

fsi0uk1n

fsi0uk1n2#

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

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

brgchamk3#

编辑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

im9ewurl

im9ewurl4#

假设我们使用ASP .NETCore/ 7来构建web API,对于.NET 7(或最小api),我们需要尝试在Program.cs中注解/调整下面的代码段

// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
//{
    app.UseSwagger();
    app.UseSwaggerUI();
//}

下一次使用VS / VS代码构建/发布并在本地开发/测试中访问您的API:https://local-api.{您的api名称}.com/{服务名称}服务/swagger/索引.html

相关问题