为什么将swagger.json作为应用程序托管在IIS上的网站下时,会出现找不到错误? [英] Why swagger.json gets not found error when hosted as an application under a website on IIS?

查看:72
本文介绍了为什么将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。

复制非常简单的设置:

  1. 新建WebAPI项目。
  2. 安装swashbacle.aspnetcore 5.0.0-Rc2
  3. 更改启动.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();
    }
}
  1. 将.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>
  1. 部署到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上的网站下时,会出现找不到错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆