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# 从 List<List<int>> 中删除重 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- WPF 模态进度窗口 2022-01-01
