托管到本地 IIS 后 ASP.NET 无法读取核心 1.2 应用程序上的 Swagger JSON 文件



托管我的 asp.net 核心 1.2 应用程序后,我收到错误为:

Swagger找不到swagger.json文件。

我试图通过app.UseSwaggerUI()提供一个虚拟路径名来解决问题,但它不起作用。

编辑以根据评论澄清问题:

在 IIS 中承载 Asp.net 核心应用程序后,swagger.json文件将localhost:<random_port>/swagger/v1/swagger.json路径上生成。

如何在自定义路由上提供swagger.json文件,例如:localhost:<random_port>/virtualpathname/swagger/v1/swagger.json

我尝试在应用程序中设置虚拟路径。像{virtualpathname}/swagger/v2/swagger.json一样使用SwaggerUI((,但它仍然不起作用

可能有几个原因 - 一个是.Net Core默认不提供静态文件(尽管查看在线示例这似乎不是问题(。

如果尚未安装包,请尝试安装包Microsoft.AspNetCore.StaticFiles,并在Startup.csConfigure()方法中添加UseStaticFiles(),使用以下配置。我不认为顺序很重要,但这是我在工作应用程序中运行的顺序。

public void Configure(...)
{
// Enable middleware to serve static files (like .json)
app.UseStaticFiles();
//Enable middleware for your API
app.UseMvc();
// Enable middleware to serve generated Swagger as a JSON endpoint
app.UseSwagger();
// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "YourApp API V1");
});
}

您还需要在ConfigureServices()方法中配置SwaggerGen中间件。

services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "api_name", Version = "1.0"});
});

编辑基于评论 - 在自定义路由上提供大摇大摆的 json:

// Enable middleware to serve generated Swagger as a JSON endpoint on a custom endpoint
app.UseSwagger(c => c.RouteTemplate = "custom/swagger/{documentName}/swagger.json");
// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
// Using custom endpoint defined above
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/custom/swagger/v1/swagger.json", "YourApp API V1");
});

如果您还需要在自定义路由上提供 SwaggerUI,则:

// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
// Using custom endpoint defined above
// And serving UI on a custom route
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/custom/swagger/v1/swagger.json", "YourApp API V1");
c.RoutePrefix = "custom"; // serves UI on http://{domain}:{port}/custom/
});

我建议您执行接下来的两个步骤。

  • 首先,打开您的项目 web.config 并启用 stdoutLogEnabled。(请记住在应用程序文件夹上创建文件夹日志并授予其适当的权限(
  • 其次,确保您正在执行正确的配置。(https://learn.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger(

请注意: 第一步将为您提供有关您面临的错误的更多详细信息。

就我而言,问题是我通过添加相对路径(../).在任何情况下,请确保先设置ConfigureServices,然后在配置时确保一切正常,UseSwagger应该在UseMvc之前,最后UseSwaggerUI

public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddSwaggerGen(c => {
c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "Utility", Version = "v1" });
});
// initialize configuration
var conf = new ConfigurationHelper(Microsoft.DotNet.PlatformAbstractions.ApplicationEnvironment.ApplicationBasePath);
Configuration = conf.Configuration; // just in case
// inject the RestApiWrapperService as singleton into the services configuration
var restService = new RestApiWrapperService(conf);
services.AddSingleton<IRestApiWrapperService>(restService);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseSwagger();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
// app.UseMvc();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
app.UseSwaggerUI(s => {
s.RoutePrefix = "help";
s.SwaggerEndpoint("../swagger/v1/swagger.json", "Utility");
s.InjectStylesheet("../css/swagger.min.css");
});

在 startup.cs class 上更改以下内容:

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

app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/MyWebsiteName/swagger/v1/swagger.json", 
"MyService.API v1");
});

[我的网站名称] 是在 IIS 中配置的应用程序的名称。

我碰巧有一个简单的复制粘贴错误! 请参阅下面代码中的第一行,if 语句 env。IsDevelopment(( 导致此部分在部署到 IIS 时无法运行。一种选择是将其注释掉!


//if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger(c =>
{
c.RouteTemplate = "swagger/{documentName}/swagger.json";
});
app.UseSwaggerUI(c => {
c.RoutePrefix = "swagger";
c.SwaggerEndpoint("/swagger/v1/swagger.json", "StockOps.WebAPI v1");                                   
});                
}

最新更新