在遵循此示例文档之后,我正在尝试弄清楚如何使用 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?配置中是否缺少某些内容?
您的
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 core托管包。
https://dotnet.microsoft.com/download/dotnet-core/thank-you/runtime-aspnetcore-3.1.6-windows-hosting-bundle-installer
如果有什么需要我帮忙的,请随时告诉我。
假设我们使用 ASP.NET Core / 7 来构建 Web api。 对于 .NET 7(或最小 api),我们需要尝试在 Program.cs 中注释/调整下面的代码片段
// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
//{
app.UseSwagger();
app.UseSwaggerUI();
//}
接下来使用 VS / VS Code 构建/发布 并在本地开发/测试上访问您的 API: https://local-api.{yur-api-name}.com/{service-name}Services/swagger/index.html
编辑您的 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();
}
补充一下,PropertyGroup 节点下的 API 项目文件中也可能缺少以下节点。
<RuntimeIdentifiers>win10-x64;</RuntimeIdentifiers>
<AspNetCoreModuleName>AspNetCoreModuleV2</AspNetCoreModuleName>
适合寻找 ASP.NET Core 版本 6/7/8 并希望通过保存远程部署时的 web.config 文件轻松打开的用户。这假设您在程序中保留了默认值。cs 或默认配置代码启动。
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
免责声明:仅在受保护的服务器上或在专用 VPN 或防火墙开发/沙箱/登台服务器后面启用此功能。切勿在生产环境中使用。 轻松启用本地/远程部署并查看 Swagger 界面进行测试。
WEB.CONFIG 示例
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\armstrongVenRestAPI.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" >
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</location>
</configuration>