I'm working on converting a web application that runs on ASP.NET MVC on .NET framework to run on .NET 8.
I see that no OWIN StartUp.cs
class is created by default. Is this just a convention, or does .NET 8 no longer use StartUp.cs
?
If I'm expected to not use a StartUp
class, then how do I normally accomplish setting up things that are normally setup in StartUp
, such as setting up OAuth?
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();
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