Why swagger.json gets not found error when hosted as an application under a website on IIS?(为什么将swagger.json作为应用程序托管在IIS上的网站下时,会出现找不到错误?)
问题描述
我尝试在Netcore2.2中使用Swashbakle.AspNetCore 5.0.0-rc2和4.0.1创建WebAPI,问题是相同的。它可以在本地计算机上运行,但当我编译发布版本并部署到IIS时,我进入站点http://localhost/mysite/并出现错误:
未找到获取错误/swagger/v1/swagger.json
另外,在浏览器中,如果我输入http://localhost/mysite/swagger/v1/swagger.json,我会在OpenApi中看到一个Json。
复制非常简单的设置:
- 新建WebAPI项目。
- 安装swashbacle.aspnetcore 5.0.0-Rc2
- 更改启动.cs:
公开课启动 { 公共启动(IConfiguration配置) { 配置=配置; )
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty;
});
app.UseMvc();
}
}
- 将.csproj更改为以下指令:
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>netcoreapp2.2</TargetFramework> <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel> </PropertyGroup> <!-- Enables XML comments on Swagger-UI --> <PropertyGroup> <GenerateDocumentationFile>true</GenerateDocumentationFile> <NoWarn>$(NoWarn);1591</NoWarn> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc2" /> </ItemGroup> </Project>
- 部署到IIS并创建应用程序池。
知道为什么库找不到那里的swagger.json吗?
推荐答案
对于swagger.json,您需要在SwaggerEndpoint点赞c.SwaggerEndpoint("/mysite/swagger/v1/swagger.json", "My API V1");之前追加网站。正如您已经发现的,您的swagger.json位于http://localhost/mysite/swagger/v1/swagger.json下,而不是http://localhost/swagger/v1/swagger.json下。
尝试更改您的配置,如下所示
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("/mysite/swagger/v1/swagger.json", "Web API V1");
#endif
c.RoutePrefix = string.Empty;
});
这篇关于为什么将swagger.json作为应用程序托管在IIS上的网站下时,会出现找不到错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么将swagger.json作为应用程序托管在IIS上的网站下时,会出现找不到错误?
基础教程推荐
- 从 C# 控制相机设备 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 将数据集转换为列表 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 如果条件可以为空 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
