-1

When I publish my ASP.NET Core 8 Minimal API project using these settings:

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

on IIS on a Windows Server 2012 R2, I always get 404 errors.

To test if IIS is actually serving something, I have created an index.html in the root of the website. When I browse to this location the index.html is served. So IIS does serve something...

I have also installed the ASP.NET Core Runtime 8.0.5 Windows Hosting Bundle.

In IIS I have an ApplicationPool with these settings:

  • .NET CLR Version v4.0.30319
  • Pipeline Mode: Integrated

According to several resources on the web this should do the trick. But in my case however I keep getting 404 errors, no matter what.

  • When accessing /swagger -> 404 (and yes I did remove the IsDevelopment() check :))
  • When accessing a GET request (/foo/1) I get a 404

What am I missing or what can I do to troubleshoot this further?

PS: I have full control over the Windows server.

Here's my 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();
6
  • Where is your code? Nothing above is very helpful for others to tell if the 404 resposes are desired. Commented Jun 29, 2024 at 8:07
  • @LexLi Added code. Commented Jun 29, 2024 at 8:21
  • Have you tried "No managed code" for the ".NET CLR version" in your IIS application pool settings ? .NET Core doesn't need any .NET version setting here - it's handled differently Commented Jun 29, 2024 at 8:41
  • @marc_s Yes, I have tried setting it to No managed code, but get exactly the same result: 404 error pages. Commented Jun 29, 2024 at 8:52
  • Error 404 is not found. IIS you do not have access to the file system on the IIS. Put the webpage(s) on the Shared Drive where the clients have access. When a HTTP connection is made to a IIS server you only have GUEST privileges. Commented Jun 29, 2024 at 9:12

2 Answers 2

0

Finally I figured out what happened.

I use SQL (no EF!) and I am using System.Data.SqlClient to connect to my MS SQL Database.

By default, in the ASP.NET Core 8 Minimal API project template, it has this property in the .csproj file: <InvariantGlobalization>true</InvariantGlobalization>

When the code hits sqlConnection.Open(); you get an exception: System.NotSupportedException: Globalization Invariant Mode is not supported

To get rid of this exception, you need to set the above property to false: <InvariantGlobalization>false</InvariantGlobalization> in the .csproj.

Now, when you have done this, and you publish the API to the file system, you won't get a web.config!

My solution was to use the web.config that is genereted when publishing API using the above setting with the value set to true. This way, a web.config is genereted. After publishing and securing the web.config file, I set the value of InvariantGlobalization back to false.

So, long story short: when setting InvariantGlobalization to false, publishing the project won't get you a web.config. You need to create one yourself and put in on the right place.

Sign up to request clarification or add additional context in comments.

Comments

-1

https://learn.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-8.0enter code here

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();

2 Comments

What is this for? Is it an answer?
I appreciate your tie and effort, but CORS has nothing to do with my issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.