With the release of .NET 6, when you create a new ASP.NET Core application, you no longer have a Startup.cs
file. Instead everything should be written inside the Program.cs
file:
If you don’t like this and you still want to use a Startup.cs
file, this is still possible.
First let’s recreate our good old Startup.cs
file:
In a first attempt, I tried the following:
Unfortunately, this didn’t work and resulted in the following error message:
Exception thrown: 'System.NotSupportedException' in Microsoft.AspNetCore.dll
An unhandled exception of type 'System.NotSupportedException' occurred in Microsoft.AspNetCore.dll
UseStartup() is not supported by WebApplicationBuilder.WebHost. Use the WebApplication returned by WebApplicationBuilder.Build() instead.
I switched to a more manual approach where I create the Startup class myself and invoke the methods directly:
Of course now it doesn’t matter anymore how the Startup.cs
file is structured or how you should name the different methods.