ASP.NET Core 8 Minimal API 在 IIS 中部署时抛出 404 错误

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

当我使用这些设置发布 ASP.NET Core 8 Minimal API 项目时:

Configuration: Release
Target framework: net8.0
Target runtime: Portable

在 Windows Server 2012 R2 上的 IIS 上,我总是收到 404 错误。

为了测试 IIS 是否确实在提供服务,我在网站的根目录中创建了一个

index.html
。当我浏览到此位置时,将提供
index.html
。所以 IIS 确实提供了一些服务...

我还安装了 ASP.NET Core Runtime 8.0.5 Windows Hosting Bundle。

在 IIS 中,我有一个具有以下设置的应用程序池:

  • .NET CLR 版本 v4.0.30319
  • 流水线模式:集成

根据网络上的一些资源,这应该可以解决问题。但就我而言,无论如何我都会收到 404 错误。

  • 访问
    /swagger
    -> 404 时(是的,我确实删除了
    IsDevelopment()
    检查:))
  • 访问 GET 请求时 (
    /foo/1
    ),我收到 404

我缺少什么或者我可以做些什么来进一步解决这个问题?

PS:我可以完全控制Windows服务器。

这是我的

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.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
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();
c# iis minimal-apis asp.net-core-8
1个回答
0
投票

https://learn.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-8.0在此处输入代码

var  MyAllowSpecificOrigins = "_myAllowSpecificOrigins";

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddCors(options =>
{
    options.AddPolicy(name: MyAllowSpecificOrigins,
                      policy  =>
                      {
                          policy.WithOrigins("http://example.com",
                                              "http://www.contoso.com");
                      });
});

// services.AddResponseCaching();

builder.Services.AddControllers();

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

app.UseCors(MyAllowSpecificOrigins);

app.UseAuthorization();

app.MapControllers();

app.Run();
© www.soinside.com 2019 - 2024. All rights reserved.