简体   繁体   English

在 IIS 上部署 API 时,Swagger UI 不显示

[英]Swagger UI not displaying when deploying API on IIS

Well, I'm using Swagger for my API documentation and it works perfectly in localhost, the problem begins when I host it on the IIS.好吧,我将 Swagger 用于我的 API 文档,它在 localhost 中完美运行,当我将它托管在 IIS 上时,问题就开始了。 For somereason it just doesn't work anymore由于某种原因,它不再起作用了

localhost:本地主机:

https://localhost:44381/swagger/index.html

api: api:

http://200.155.29.14/SimuladorFrete/Swagger/index.html

All I get when I try to open the Swagger after deploying it in the ISS is a blank page and a 500 internal server error, which doesn't say the exception.当我在 ISS 中部署 Swagger 后尝试打开它时,我得到的只是一个空白页和一个 500 内部服务器错误,这并没有说明异常。

Here is my Configure method (startup.cs)这是我的配置方法(startup.cs)

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();

    }
    app.UseHttpsRedirection();

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
    app.UseSwagger();
    app.UseStaticFiles();
    app.UseSwaggerUI(c =>
    {
        if (env.IsDevelopment())
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "Web API V1");
        }
        else
        {
            // To deploy on IIS
            c.SwaggerEndpoint("/SimulaFrete/swagger/v1/swagger.json", "Web API V1");
        }

    });
}

and here's my ConfigureServices method:这是我的 ConfigureServices 方法:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers().AddNewtonsoftJson();

    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1",

      new OpenApiInfo
      {

          Title = "Simulador de frete por Unidade",
          Version = "v1",
          Description = "Métodos da API de simulação de frete",
          Contact = new OpenApiContact
          {
              Name = "Catalde Technology",
              Url = new Uri("https://www.catalde.com"),
              Email = "cicero.catalde@catalde.com"
          }
      });

        string caminhoAplicacao =
            PlatformServices.Default.Application.ApplicationBasePath;
        string nomeAplicacao =
            PlatformServices.Default.Application.ApplicationName;
        string caminhoXmlDoc =
            Path.Combine(caminhoAplicacao, $"{nomeAplicacao}.xml");

        c.IncludeXmlComments(caminhoXmlDoc);
        c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First()); //This line
    });
}

And there's my Controller code which has only this method:还有我的 Controller 代码只有这个方法:

[HttpPost]
public ContentResult Post([FromBody]dadosFrete xml)
{
    // code logic
}

You need to temporarily add the production clause in your condition before you can see the swagger in the production environment.您需要在您的条件中临时添加生产子句,然后才能在生产环境中看到 swagger。 See the yellow highlighted section in the attached image.请参阅附图中黄色突出显示的部分。 env.IsProduction() env.IsProduction()

图片

Try this.尝试这个。 To set relative path.设置相对路径。

app.UseSwaggerUI(c =>
    {
        string swaggerJsonBasePath = string.IsNullOrWhiteSpace(c.RoutePrefix) ? "." : "..";
        c.SwaggerEndpoint($"{swaggerJsonBasePath}/swagger/v1/swagger.json", "My API");

    });

if this solution not work.如果此解决方案不起作用。 Then check publish setting.然后检查发布设置。 The issue is that running dotnet publish with -r Release does not produce XML file.问题是使用-r Release运行dotnet publish不会生成 XML 文件。 However, dotnet publish with -r Debug does in fact produce the file.但是,带有-r Debugdotnet publish实际上会生成该文件。 This explains why people are only getting this issue when they are deploying to environments OTHER than locally.这解释了为什么人们只有在部署到本地环境以外的环境时才会遇到这个问题。 You can simplify find out in Project > Your Project properties > Build Tab您可以在Project > Your Project properties > Build Tab中简化查找

Take a look at how to use multiple environments in .net core.看看如何在.net内核中使用多个环境 Some options are一些选项是

  • using a launch.json file for your different environments (dev/stage/prod) (local dev only)为您的不同环境(dev/stage/prod)使用launch.json文件(仅限本地开发)
  • Adding an environment variable named ASPNETCORE_ENVIRONMENT with development , stage or production添加名为ASPNETCORE_ENVIRONMENT的环境变量,包括developmentstageproduction
  • Or making a web.config to deploy to IIS and add the value或者制作一个web.config部署到 IIS 并添加值
<PropertyGroup>
<EnvironmentName>Development</EnvironmentName>
</PropertyGroup>

For IIS, it all depends on the version of IIS.对于 IIS,这一切都取决于 IIS 的版本。 Older versions of IIS, I believe you can only go with web.config.旧版本的IIS,相信你只能用go和web.config。

Simply edit your csproj file and add the following snippet只需编辑您的 csproj 文件并添加以下代码段

<PropertyGroup>
 <EnvironmentName>Development</EnvironmentName>
</PropertyGroup>

The following worked for me.以下对我有用。 I was able to get it working locally on IIS.我能够让它在 IIS 上本地工作。

  1. You'll need to publish the project to a folder and point IIS to that folder.您需要将项目发布到一个文件夹并将 IIS 指向该文件夹。
  2. Edit the Configure method and move out Swagger out of env.IsDevelopment and use vipul's solution above to fix the path.编辑 Configure 方法,将 Swagger 移出 env.IsDevelopment 并使用上面 vipul 的解决方案修复路径。
 if (env.IsDevelopment())
 {
   app.UseDeveloperExceptionPage();
 }
 app.UseSwagger();
 app.UseSwaggerUI(c =>
 {
   string swaggerJsonBasePath = string.IsNullOrWhiteSpace(c.RoutePrefix) ? "." : "..";
   c.SwaggerEndpoint($"{swaggerJsonBasePath}/swagger/v1/swagger.json", "Web API");

 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM