I am attempting to convert our public API into using just the single Program.cs format as part of our .NET8 update but despite the application building and running, when I try to hit a route I get a 404 and in the debug output I get "Request reached the end of the middleware pipeline without being handled by application code." I have stripped out all but the default "app.Xyz" that come with the dotnet template but the issue still remains. My "app" section of the Program.cs now looks like this:
var app = builder.Build();
var listener = app.Services.GetRequiredService<DiagnosticListener>();
var observer = ActivatorUtilities.CreateInstance<AnalysisDiagnosticAdapter>(app.Services);
using var disposable = listener.SubscribeWithAdapter(observer);
app.UseHttpsRedirection();
app.UseRouting();
app.UseUserAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "Default",
pattern:"{controller=Home}/{action=Index}/{id?}"
);
app.Run();
I added in the DiagnosticAdapter to see if that would help but it doesn't really tell me anything that the default debug output did.
I can add a test route in this file, using app.MapGet();
which works and returns correctly but any controller external to this return the 404 error.
You should register the controllers. Try to replacing app building line with below:
builder.Services.AddControllers();
var app = builder.Build();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With