托管到本地 IIS 后无法读取 ASP.NET Core 1.2 应用程序上的 swagger JSON 文件
Can't read swagger JSON file on ASP.NET Core 1.2 Application after hosting into local IIS
托管我的 asp.net 核心 1.2 应用程序后,我收到如下错误:
swagger is unable to find the swagger.json
file.
我试图通过提供虚拟路径名来解决问题 app.UseSwaggerUI()
但它不起作用。
编辑以根据评论澄清问题:
在 IIS 中托管 Asp.net 核心应用程序后,swagger.json
文件正在 localhost:<random_port>/swagger/v1/swagger.json
路径上生成。
如何在自定义路由上提供 swagger.json
文件,例如:
localhost:<random_port>/virtualpathname/swagger/v1/swagger.json
我试过在 app.UseSwaggerUI() 中设置一个虚拟路径,就像 {virtualpathname}/swagger/v2/swagger.json
但它仍然不起作用
我建议您执行接下来的两个步骤。
- 首先,打开您的项目 web.config 并启用 stdoutLogEnabled。 (记得在您的应用程序文件夹上创建文件夹日志并赋予它适当的权限)
- 其次,确保您的配置正确。 (https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger)
注意:第一步是为您提供有关您遇到的错误的更多详细信息。
这可能有几个原因 - 一个是 .Net Core 默认不提供静态文件(尽管查看在线示例这似乎不是问题)。
如果还没有,请尝试安装包 Microsoft.AspNetCore.StaticFiles
并使用以下配置在 Startup.cs
中的 Configure()
方法中添加 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"});
});
编辑 根据评论 - 在自定义路线上提供 swagger 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/
});
在我的例子中,问题是我通过添加相对路径 (../) 修复的虚拟目录。在任何情况下,请确保先设置 ConfigureServices,然后在 Configure 时确保一切正常,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");
});
[MyWebsiteName] 是在 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");
});
}
托管我的 asp.net 核心 1.2 应用程序后,我收到如下错误:
swagger is unable to find the
swagger.json
file.
我试图通过提供虚拟路径名来解决问题 app.UseSwaggerUI()
但它不起作用。
编辑以根据评论澄清问题:
在 IIS 中托管 Asp.net 核心应用程序后,swagger.json
文件正在 localhost:<random_port>/swagger/v1/swagger.json
路径上生成。
如何在自定义路由上提供 swagger.json
文件,例如:
localhost:<random_port>/virtualpathname/swagger/v1/swagger.json
我试过在 app.UseSwaggerUI() 中设置一个虚拟路径,就像 {virtualpathname}/swagger/v2/swagger.json
但它仍然不起作用
我建议您执行接下来的两个步骤。
- 首先,打开您的项目 web.config 并启用 stdoutLogEnabled。 (记得在您的应用程序文件夹上创建文件夹日志并赋予它适当的权限)
- 其次,确保您的配置正确。 (https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger)
注意:第一步是为您提供有关您遇到的错误的更多详细信息。
这可能有几个原因 - 一个是 .Net Core 默认不提供静态文件(尽管查看在线示例这似乎不是问题)。
如果还没有,请尝试安装包 Microsoft.AspNetCore.StaticFiles
并使用以下配置在 Startup.cs
中的 Configure()
方法中添加 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"});
});
编辑 根据评论 - 在自定义路线上提供 swagger 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/
});
在我的例子中,问题是我通过添加相对路径 (../) 修复的虚拟目录。在任何情况下,请确保先设置 ConfigureServices,然后在 Configure 时确保一切正常,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");
});
[MyWebsiteName] 是在 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");
});
}