为什么 swagger.json 在作为应用程序托管在 IIS 网站下时出现找不到错误?
Why swagger.json gets not found error when hosted as an application under a website on IIS?
我尝试在 Netcore 2.2 中使用 Swashbuckle.AspNetCore 5.0.0-rc2 和 4.0.1 创建一个 Webapi,问题是一样的。它在本地机器上工作,但是当我编译发布版本并部署到 IIS 时,我输入站点 http://localhost/mysite/ 和错误:
Fetch error Not Found /swagger/v1/swagger.json
此外,在浏览器中,如果我输入 http://localhost/mysite/swagger/v1/swagger.json,我会在 OpenApi 中看到 Json。
重现的设置非常简单:
- 创建一个新的 webapi 项目。
- 安装swashbuckle.aspnetcore
5.0.0-rc2
- 更改 startup.cs:
public class 启动
{
public Startup(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;
});
如果您将 DTO 与 JsonProperty 一起使用,则必须检查 属性 名称中是否存在任何重复。
public class SampleDto
{
[JsonProperty(PropertyName = "Building", NullValueHandling = NullValueHandling.Ignore)]
public string Building { get; set; }
[JsonProperty(PropertyName = "Comment", NullValueHandling = NullValueHandling.Ignore)]
public string Comment { get; set; }
[JsonProperty(PropertyName = "Comment", NullValueHandling = NullValueHandling.Ignore)]
public string Country { get; set; }
}
在上面的代码中,JsonProperty 的名称为“Comment”两次。这将引发 500 错误。
通过将“评论”json 属性名称修改为“国家 " 就像 dto 属性name 一样,代码将起作用。 (请参阅下面调整后的代码)
public class SampleDto
{
[JsonProperty(PropertyName = "Building", NullValueHandling = NullValueHandling.Ignore)]
public string Building { get; set; }
[JsonProperty(PropertyName = "Comment", NullValueHandling = NullValueHandling.Ignore)]
public string Comment { get; set; }
[JsonProperty(PropertyName = "Country ", NullValueHandling = NullValueHandling.Ignore)]
public string Country { get; set; }
}
我尝试在 Netcore 2.2 中使用 Swashbuckle.AspNetCore 5.0.0-rc2 和 4.0.1 创建一个 Webapi,问题是一样的。它在本地机器上工作,但是当我编译发布版本并部署到 IIS 时,我输入站点 http://localhost/mysite/ 和错误:
Fetch error Not Found /swagger/v1/swagger.json
此外,在浏览器中,如果我输入 http://localhost/mysite/swagger/v1/swagger.json,我会在 OpenApi 中看到 Json。
重现的设置非常简单:
- 创建一个新的 webapi 项目。
- 安装swashbuckle.aspnetcore 5.0.0-rc2
- 更改 startup.cs:
public class 启动 { public Startup(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;
});
如果您将 DTO 与 JsonProperty 一起使用,则必须检查 属性 名称中是否存在任何重复。
public class SampleDto
{
[JsonProperty(PropertyName = "Building", NullValueHandling = NullValueHandling.Ignore)]
public string Building { get; set; }
[JsonProperty(PropertyName = "Comment", NullValueHandling = NullValueHandling.Ignore)]
public string Comment { get; set; }
[JsonProperty(PropertyName = "Comment", NullValueHandling = NullValueHandling.Ignore)]
public string Country { get; set; }
}
在上面的代码中,JsonProperty 的名称为“Comment”两次。这将引发 500 错误。
通过将“评论”json 属性名称修改为“国家 " 就像 dto 属性name 一样,代码将起作用。 (请参阅下面调整后的代码)
public class SampleDto
{
[JsonProperty(PropertyName = "Building", NullValueHandling = NullValueHandling.Ignore)]
public string Building { get; set; }
[JsonProperty(PropertyName = "Comment", NullValueHandling = NullValueHandling.Ignore)]
public string Comment { get; set; }
[JsonProperty(PropertyName = "Country ", NullValueHandling = NullValueHandling.Ignore)]
public string Country { get; set; }
}