带有Swagger UI的ASP.NET Core 6在IIS Web服务器上无法工作

z2acfund  于 2023-08-05  发布在  .NET
关注(0)|答案(1)|浏览(206)

我的ASP.NETCore6.0站点在IIS服务器上发布时返回“找不到页面”错误。
我将我的网站发布到一个文件夹(D:\APIsite),然后将其设置为IIS。已尝试调试和发布配置。
下面是program.cs中的代码:

var builder = WebApplication.CreateBuilder(args);

Log.Logger = new LoggerConfiguration().WriteTo.File(new MyOwnCompactJsonFormatter(), "API_Log.txt").CreateLogger();

builder.Services.AddControllers();

builder.Services.AddEndpointsApiExplorer();

builder.Services.AddSingleton<CheckPointMetraContext>();
builder.Services.AddSingleton<CheckPointPackagingContext>();
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo
    {
        Version = "v1",
        Title = "ToDo API",
        Description = "A simple example ASP.NET Core Web API",
    });

});

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseSwagger(c => { c.SerializeAsV2 = true; });
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        c.RoutePrefix = string.Empty;
    });
}

app.UseAuthorization();

app.MapControllers();

app.UseRouting();

Log.Logger.Information("API http://localhost:5139/swagger/index.html");

app.Run();

字符串
launchSettings.json中的配置:

{
  
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:31124",
      "sslPort": 44300
    }
  },
  "profiles": {
    "TestApp": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
     
      "applicationUrl": "https://localhost:7003;http://localhost:5139",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}


IIS站点绑定

http   5139 127.0.0.1
https  7003 127.0.0.1


文件夹到应用程序D:\APIsite
当进入网站的授权部分工作正常,但随后页面找不到错误.
当调试或发布第一页URL为https://localhost:7003/index.html时(一切正常)
在IIS中打开时,第一个页面URL为https://127.0.0.1:7003(授权后未找到页面)
即使我从debug请求正确的URL,我也会得到“Page not found”。
另一个ASP.NET应用程序在IIS上工作正常

jv2fixgn

jv2fixgn1#

我知道了第一个问题是在我的LaunchSettings.json中。applicationUrl必须与IIS绑定中的相同。所以如果在IIS中我有127.0.0.1,那么在我的启动设置中我必须写127.0.0.1。第二个是program.cs。我应该转身的

if (app.Environment.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseSwagger(c => { c.SerializeAsV2 = true; });
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            c.RoutePrefix = string.Empty;
        });
    }

字符串
到这个

app.UseSwagger();
    app.UseSwaggerUI();


所以没有任何条件if(app.Environment.IsDevelopment())

相关问题