IIS 上的 ASP.NET Core 8.0 Web API - 404 未找到(已更新!)

问题描述 投票:0回答:1

更新

我的 Web API 不再报告 404 Not Found。在调试中,我发现了一个 SwaggerGeneratorException 错误,指出“无法为类型 - [TypeName] 生成架构...无法使用 schemaId...相同的 schemaId 已用于类型 [TypeName]”

为了解决这个问题,我更换了

builder.Services.AddSwaggerGen();

builder.Services.AddSwaggerGen(options =>
{
    options.CustomSchemaIds(type => type.ToString());
});

重建并发布项目后,我现在收到 500 内部服务器错误。完整的错误消息显示:

HTTP 错误 500.31 - 无法加载 ASP.NET Core 运行时 常见 该问题的解决方案:指定版本 找不到 Microsoft.NetCore.App 或 Microsoft.AspNetCore.App。 故障排除步骤:检查系统事件日志中是否有错误消息 启用记录应用程序进程的标准输出消息 调试器到应用程序进程并检查

应用日志

错误:无法启动应用程序“/LM/W3SVC/5/ROOT”,错误代码“0x8000ffff”。

错误:找不到“aspnetcorev2_inprocess.dll”。异常消息: 无法找到任何兼容的框架版本 找不到框架“Microsoft.NETCore.App”,版本“8.0.0”。

  • 发现以下框架: 3.1.19 在 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] 5.0.10 在 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

您可以通过安装指定的框架和/或SDK来解决该问题。指定的框架可以在:

错误:无法找到应用程序依赖项。确保安装了应用程序所针对的 Microsoft.NetCore.App 和 Microsoft.AspNetCore.App 版本。

警告:无法使用应用程序基“C:\inetpub\wwwroot\SamadhiAU_API”启动标准输出文件重定向到“.\logs\stdout”。 create_directories:访问被拒绝。:“C:\inetpub\wwwroot\myAPIFolder.\logs”。

原帖

我正在将一个 Web API 项目从 Core 3.1 迁移到 .NET 8.0。我在 VS 2022 中创建了一个新的 Web API 项目并移植了所有类。我删除了所有过时的依赖项并根据需要添加了新的依赖项。

项目构建成功,在调试中运行时,我可以使用 Postman 调用各种端点,一切都按预期工作。

我在 IIS 中创建了一个新站点,添加了适当的绑定,还安装了适当的 SSL 证书。我使用了服务器名称指示,因为我已经在 443 上有一个现有的 API,尽管具有不同的域。

我将项目发布到本地文件夹,然后将文件复制到 IIS 中的正确文件夹。

当我打开 postman 测试 API 时,我得到 404 Not Found

在调试中,调用 POST:https://localhost:7297/users/authenticate 我得到了预期的 JSON 响应。

在生产中,https://mydomain/users/authenticate 返回 HTML 和 404 Not Found。

程序.cs

using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using SamadhiAPI;
using SamadhiAPI.API;
using SamadhiAPI.Models;
using System.Text;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container    
builder.Services.AddCors();

builder.Services.AddControllers();
// learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// configure strongly typed settings objects
var appSettingsSection = builder.Configuration.GetSection("AppSettings");
builder.Services.Configure<AppSettings>(appSettingsSection);

builder.Services.AddScoped<IUserService, UserService>();

// configure jwt authentication
var appSettings = appSettingsSection.Get<AppSettings>();
var key = Encoding.ASCII.GetBytes(appSettings.Secret);
builder.Services.AddAuthentication(x =>
{
    x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
    x.RequireHttpsMetadata = false;
    x.SaveToken = true;
    x.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuerSigningKey = true,
        IssuerSigningKey = new SymmetricSecurityKey(key),
        ValidateIssuer = false,
        ValidateAudience = false,
        // set clockskew to zero so tokens expire exactly at token expiration time (instead of 5 minutes later)
        ClockSkew = TimeSpan.Zero
    };
});

var app = builder.Build();
app.UseRouting();

// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
    app.UseDeveloperExceptionPage();
}

// global cors policy
app.UseCors(x => x
    .SetIsOriginAllowed(origin => true)
    .AllowAnyMethod()
    .AllowAnyHeader()
    .AllowCredentials());

app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();

web.config

自动生成并保持不变。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\SamadhiAPI.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
</configuration>
<!--ProjectGuid: b7f93657-cfc6-4b8d-91d7-33f7e9e4960b-->

采取的步骤

  1. 检查的域实际上指向我的 IIS 服务器
  2. 我可以看到我的网站列在 IIS 工作进程中
  3. 日志文件中的条目显示了我的请求(见下文)

日志文件:/inetpub/logs/日志文件/W3SVC5

#Software: Microsoft Internet Information Services 10.0
#Version: 1.0
#Date: 2024-05-27 08:04:36
#Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Referer) sc-status sc-substatus sc-win32-status time-taken
2024-05-27 08:04:36 xxx.x.xx.xxx GET / - 80 - 199.45.155.43 Mozilla/5.0+(compatible;+CensysInspect/1.1;++https://about.censys.io/) - 500 31 2147549183 615
#Software: Microsoft Internet Information Services 10.0
#Version: 1.0
#Date: 2024-05-27 08:54:12
#Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Referer) sc-status sc-substatus sc-win32-status time-taken
2024-05-27 08:54:12 xxx.x.xx.xxx POST /users/authenticate - 443 - 159.196.169.55 PostmanRuntime/7.34.0 - 500 31 2147549183 505
2024-05-27 08:54:31 xxx.x.xx.xxx POST /users/authenticate - 443 - 159.196.169.55 PostmanRuntime/7.34.0 - 500 31 2147549183 9
c# iis asp.net-web-api visual-studio-2022 windows-server
1个回答
0
投票

我原来的错误消息是 404 Not Found。在调试中,我发现此错误是由 SwaggerGeneratorException 错误引起的,该错误显示“无法为类型 - [TypeName] 生成架构...无法使用 schemaId...相同的 schemaId 已用于类型 [TypeName]”

要修复 Program.cs 中的此错误,我替换了

builder.Services.AddSwaggerGen();

builder.Services.AddSwaggerGen(options =>
{
    options.CustomSchemaIds(type => type.ToString());
});

重建并重新发布项目后,我收到一个新错误“500 内部服务器错误”,提示“无法加载 ASP.NET Core 运行时”。检查 Windows 服务器“应用程序”事件日志后,我发现了各种错误消息,包括:

  1. 找不到“aspnetcorev2_inprocess.dll”
  2. 无法启动应用程序“/LM/W3SVC/5/ROOT”,错误代码“0x8000ffff”。
  3. 无法找到应用程序依赖项。确保安装了应用程序所针对的 Microsoft.NetCore.App 和 Microsoft.AspNetCore.App 版本。

通过从以下 URL 安装 ASP.NET Core Runtime 8.0.5 Windows Hosting Bundle 修复了此错误: https://dotnet.microsoft.com/en-us/download/dotnet/8.0

一路上的一些转移注意力的事情包括:

  1. 应用程序池没有访问站点文件夹的权限。
  2. 一位用户报告说,他在从 Visual Studio 发布时忽略设置“自包含”部署模式,从而出现了上述错误。

我不需要将部署模式从默认值“依赖于框架”和目标运行时“可移植”更改。

© www.soinside.com 2019 - 2024. All rights reserved.