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

.NET Core 6部署IIS后POST请求返回404问题求助

部署到IIS后POST接口失效排查

问题描述

本地测试POST接口完全正常,但部署到IIS服务器后所有POST请求均失效,GET方法在本地和服务器上均可正常运行。请求携带API密钥,服务器仅提示“找不到指定的URL”,请求未到达接口。

接口代码

[HttpPost("storesubmission")]
public async Task<IActionResult> StoreSubmission([FromBody]TestModel tModel)
{
        mt.name = tModel.name;
        mt.age = tModel.age;
        mt.submittedBy = tModel.submittedBy;
        mt.submittedAt = tModel.submittedAt;
        mt.province = tModel.province;
        mt.tractId = tModel.tractId;  
        mt.referenceNumber = tModel.referenceNumber;
        _context.TestModels.Add(mt);

        await _context.SaveChangesAsync();

    return Ok(mt);
}

Program.cs代码

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<DataContext>(options =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("ProntoConnection"));
});


var corsPolicy = "_corsPolicy";
builder.Services.AddCors(options =>
{
    options.AddPolicy(corsPolicy,
                      policy =>
                      {
                          policy
                               .WithOrigins("https://example.com")
                               .AllowAnyMethod()
                              .AllowAnyHeader()
                              .AllowCredentials();
                      });
});


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

app.UseCors(corsPolicy);
app.UseAuthentication();
app.UseAuthorization();
app.UseHttpsRedirection();
app.UseMiddleware<ApiKeyMiddleware>();
app.MapControllers();
app.Run();

Postman请求截图

Postman请求截图

排查方向

  • IIS请求筛选配置:检查IIS站点的请求筛选规则,确认未禁用POST方法;查看请求长度限制,若请求体超过阈值会被拦截。
  • IIS应用程序池模式:确保应用程序池使用集成模式,经典模式可能导致ASP.NET Core路由解析异常,引发404错误。
  • 中间件顺序错误:当前UseHttpsRedirection在认证和自定义中间件之后,HTTP转HTTPS时可能丢失POST请求数据或头信息。调整顺序为:UseHttpsRedirection → UseCors → UseAuthentication → UseAuthorization → UseMiddleware<ApiKeyMiddleware> → MapControllers。
  • API密钥中间件拦截:检查ApiKeyMiddleware的逻辑,确认是否对POST请求的密钥验证逻辑有误,导致请求被拦截却未返回正确错误,伪装成404。
  • 虚拟目录路径问题:若IIS站点部署在虚拟目录下,请求URL需加上虚拟目录名称,比如原本地请求/storesubmission,服务器需改为/虚拟目录名/storesubmission。
  • IIS权限验证:确认应用程序池标识对站点根目录、数据库有读写权限,权限不足可能导致隐性失败(虽通常返回500,但需排除)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 19:39:23