In ASP.NET Core (which includes .NET 8), the Startup.cs
class is still a central part of the application's configuration, but it's not the only way to set up your application. The newer versions of .NET Core have introduced a more flexible approach using the Program.cs
file.
Here's how it generally works:
Program.cs file now often contains the configuration code that you would traditionally put in Startup.cs
. It's where you create the host for the web application and can configure services and the app pipeline. This is where you could set up OAuth and other middleware components.
While the Startup.cs
class is still supported and can be used, it's no longer required. You can choose to keep your configuration in Program.cs
for simplicity, or split it between Program.cs
and Startup.cs
for better organization, especially for larger applications.
If you're setting up OAuth, this would typically be done in the ConfigureServices
method (whether in Program.cs
or Startup.cs
). You would use the services.AddAuthentication()
method to configure the authentication services.
Here’s a basic example of what the Program.cs
might look like in a .NET 8 application:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddAuthentication()
.AddOAuth(/* ... OAuth options ... */);
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
Startup
class used in .NET is different from OWIN and became optional since .NET 6. You can specify the same settings in Main now, or in extension methods. You can still use a Startup class with the UseStartup call – Term