You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core 8 Minimal API部署IIS后出现404错误,求排查方案

部署ASP.NET Core 8 Minimal API至Windows Server 2012 R2 IIS后持续404问题排查

发布与部署基础配置

  • 发布参数:
    • Configuration: Release
    • Target framework: net8.0
    • Target runtime: Portable
  • 部署环境:Windows Server 2012 R2 IIS
  • 已验证:IIS服务正常(网站根目录创建index.html可正常访问)
  • 已安装:ASP.NET Core Runtime 8.0.5 Windows Hosting Bundle
  • 应用程序池当前设置:
    • .NET CLR版本:v4.0.30319
    • 管道模式:集成

问题现象

  • 移除IsDevelopment()判断后,访问/swagger仍返回404
  • 访问API定义的路由(如GET请求/todo/2024-06-01)返回404(注:原测试的/foo/1未在代码中定义,属于无效路由)

项目Program.cs代码

using MediatR;
using ProjectLifeApi.Application.CreateToDoItem;
using ProjectLifeApi.Application.GetToDoItems;
using ProjectLifeApi.Infrastructure;
using ProjectLifeApi.Infrastructure.CreateTask;
using ProjectLifeApi.Infrastructure.GetToDoItems;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
    options.CustomSchemaIds(type => type.ToString());
});

builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblyContaining<SqlSaveToDoItem>());

builder.Services.AddTransient<ISaveToDoItem, SqlSaveToDoItem>();
builder.Services.AddTransient<IGetToDoItems, SqlGetToDoItems>();

var dbSettings = new DatabaseSettings();
builder.Configuration.GetSection("Database").Bind(dbSettings);
builder.Services.AddSingleton<DatabaseSettings>(dbSettings);

var app = builder.Build();

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

//app.UseHttpsRedirection();

app.MapPost("/todo", async (CreateToDoItemCommand command, ISender sender) =>
{
    await sender.Send(command);
});

app.MapGet("/todo/{date}", async (DateOnly date, ISender sender) => 
{
    return await sender.Send(new GetToDoItemsQuery { Date = date });
});

app.Run();

遗漏配置与排查步骤

  1. 修正应用程序池CLR版本设置
    ASP.NET Core应用为自托管模式,IIS仅作反向代理,应用程序池的正确设置应为:

    • .NET CLR版本:无托管代码
    • 管道模式:集成
      修改后重启应用程序池和网站。
  2. 验证ASP.NET Core模块与处理程序映射

    • 打开IIS管理器,进入网站的「模块」列表,确认存在AspNetCoreModuleV2;若不存在,重新安装Hosting Bundle后重启IIS。
    • 进入网站的「处理程序映射」,确认有针对*的AspNetCoreModuleV2条目:请求路径*,模块AspNetCoreModuleV2,名称设为AspNetCore;若无则手动添加。
  3. 检查web.config文件有效性
    发布后网站根目录会自动生成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=".\ProjectLifeApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
        </system.webServer>
      </location>
    </configuration>
    
    • 确认arguments中的dll文件名与项目输出一致;
    • 开启stdoutLogEnabled="true",手动创建logs文件夹并赋予应用程序池身份写入权限,查看日志排查启动错误。
  4. 验证应用程序池身份权限

    • 确保应用程序池身份(默认ApplicationPoolIdentity)对网站根目录有读取、执行权限;
    • 若API需访问数据库,需确保该身份拥有对应数据库的访问权限。
  5. 检查服务器.NET运行时版本
    打开命令提示符执行dotnet --version,确认安装的是8.0.5或兼容版本,版本不符则重新安装对应Hosting Bundle。

  6. 确认API路由测试正确性
    代码中定义的有效路由为:

    • POST /todo
    • GET /todo/{date}(date需符合yyyy-MM-dd格式,如/todo/2024-06-01)
      测试时需使用上述有效路由,避免访问未定义的路由(如/foo/1)。

内容的提问来源于stack exchange,提问作者Martijn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 20:34:53