Swagger UI 未加载
Swagger UI Not Loading
Swagger UI 未加载,Json 正在按预期加载,但受支持的 js、css 文件存在问题。
尝试复制项目中 vendor/swagger-api/swagger-ui 中的 /dist 目录。我不确定正确的方法,但我遇到了同样的问题并且它对我有用。此外,请尝试提供您所面临问题的更多详细信息,也许还有代码片段。
或者,尝试以下操作:
- 检查您的所有控制器方法是否都有 [http] 标记。如果它们都起作用但仍然不起作用,请转到步骤 2
- 在你的配置函数中确保你有 app.UseStaticFiles();如果仍然无效,请转到步骤 3
- 卸载并重新安装 swagger。如果它不起作用,请转到第 4 步(仅限核心)
- 如果您正在使用核心安装 Microsoft.AspNetCore.StaticFiles 并在您的项目中引用它。
此问题背后的原因您需要按照以下步骤操作:
在 Startup.cs 文件下有一个方法 "ConfigureServices" 在此执行以下操作:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
// Configure database connection
services.Configure<Settings>(options =>
{
options.ConnectionString = Configuration.GetSection("database:ConnectionString").Value;
options.Database = Configuration.GetSection("Db:Database").Value;
});
//register the RecordedMediaContext dependency here
services.AddTransient<ITestService, TestService>();
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
// 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);
});
// Max file upload sixe 5gb =5,368,709,120 bytes
services.Configure<FormOptions>(x => x.MultipartBodyLengthLimit = 5368709120);
}
然后在同一个Startup.cs文件下的Configure方法下添加如下代码
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
string baseApiUrl = Configuration.GetSection("BaseApiUrl").Value;
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
#if DEBUG
// For Debug in Kestrel
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Web API V1");
#else
// To deploy on IIS
c.SwaggerEndpoint(""+baseApiUrl+"/swagger/v1/swagger.json", "My API V1");
#endif
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
//Accept All HTTP Request Methods from all origins
//app.UseCors(builder => builder.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod());
app.UseHttpsRedirection();
#if !DEBUG
app.UseDefaultFiles();
app.UseStaticFiles();
#endif
app.UseMvc();
}
谢谢。
您是否在启动时启用了 StaticFiles?
app.UseStaticFiles();
我遇到了同样的问题:Swagger 在本地运行良好,但在发布到 IIS 后不显示 UI。通过向 IIS 的请求过滤功能添加允许的 .js
和 .json
文件解决了该问题。默认情况下,新创建的网站不会列出这些文件。
尝试删除 .vs 文件夹 visual studio 将在构建项目时自动重新创建它。删除后 Swagger 页面正常工作。
根据 Tyler Findlay 的回答。
我在 msdoc 上关注最初的 guide 试图启用 swagger 支持,在使用 RoutePrefix 添加此块后,我没有意识到路径从 http://localhost:<port>/swagger
到 http://localhost:<port>
,感谢 Sampat 指出。
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty; // this changes the default path
});
Swagger UI 未加载,Json 正在按预期加载,但受支持的 js、css 文件存在问题。
尝试复制项目中 vendor/swagger-api/swagger-ui 中的 /dist 目录。我不确定正确的方法,但我遇到了同样的问题并且它对我有用。此外,请尝试提供您所面临问题的更多详细信息,也许还有代码片段。 或者,尝试以下操作:
- 检查您的所有控制器方法是否都有 [http] 标记。如果它们都起作用但仍然不起作用,请转到步骤 2
- 在你的配置函数中确保你有 app.UseStaticFiles();如果仍然无效,请转到步骤 3
- 卸载并重新安装 swagger。如果它不起作用,请转到第 4 步(仅限核心)
- 如果您正在使用核心安装 Microsoft.AspNetCore.StaticFiles 并在您的项目中引用它。
此问题背后的原因您需要按照以下步骤操作:
在 Startup.cs 文件下有一个方法 "ConfigureServices" 在此执行以下操作:
public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); // Configure database connection services.Configure<Settings>(options => { options.ConnectionString = Configuration.GetSection("database:ConnectionString").Value; options.Database = Configuration.GetSection("Db:Database").Value; }); //register the RecordedMediaContext dependency here services.AddTransient<ITestService, TestService>(); // Register the Swagger generator, defining 1 or more Swagger documents services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" }); // 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); }); // Max file upload sixe 5gb =5,368,709,120 bytes services.Configure<FormOptions>(x => x.MultipartBodyLengthLimit = 5368709120); }
然后在同一个Startup.cs文件下的Configure方法下添加如下代码
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { string baseApiUrl = Configuration.GetSection("BaseApiUrl").Value; // Enable middleware to serve generated Swagger as a JSON endpoint. app.UseSwagger(); // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), // specifying the Swagger JSON endpoint. app.UseSwaggerUI(c => { #if DEBUG // For Debug in Kestrel c.SwaggerEndpoint("/swagger/v1/swagger.json", "Web API V1"); #else // To deploy on IIS c.SwaggerEndpoint(""+baseApiUrl+"/swagger/v1/swagger.json", "My API V1"); #endif }); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } //Accept All HTTP Request Methods from all origins //app.UseCors(builder => builder.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod()); app.UseHttpsRedirection(); #if !DEBUG app.UseDefaultFiles(); app.UseStaticFiles(); #endif app.UseMvc(); }
谢谢。
您是否在启动时启用了 StaticFiles?
app.UseStaticFiles();
我遇到了同样的问题:Swagger 在本地运行良好,但在发布到 IIS 后不显示 UI。通过向 IIS 的请求过滤功能添加允许的 .js
和 .json
文件解决了该问题。默认情况下,新创建的网站不会列出这些文件。
尝试删除 .vs 文件夹 visual studio 将在构建项目时自动重新创建它。删除后 Swagger 页面正常工作。
根据 Tyler Findlay 的回答。
我在 msdoc 上关注最初的 guide 试图启用 swagger 支持,在使用 RoutePrefix 添加此块后,我没有意识到路径从 http://localhost:<port>/swagger
到 http://localhost:<port>
,感谢 Sampat 指出。
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty; // this changes the default path
});