Does an ASP.NET Core 8 application use a StartUp.cs file?
Asked Answered
Y

3

14

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?

Yaws answered 14/12, 2023 at 18:34 Comment(3)
Just curious why one would want to convert from those types of .NET ? I can understand a VB Classic to some form of .NET upgrade, but what things does 8 have that MVC was limited?Salleysalli
.NET 8 is .NET Core 8. The 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 callTerm
@Salleysalli the comment isn't very clear. There are a ton of new features, performance and programming improvements in .NET Core, especially in .NET 8. All new development happens on .NET Core now. .NET Framework is in maintenance mode. Add cross-platform support, and it's hard to justify remaining in .NET FrameworkTerm
F
16

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();
Fouquiertinville answered 15/12, 2023 at 3:55 Comment(2)
How I can get Configuration in Program.cs?Furst
builder.Configuration["TestConfig:Key"]Parada
H
1

Yeah it does. You can do it just like that:

namespace Api;

public static class Program
{
    public static void Main(string[] args)
    {
        var builder = CreateHostBuilder(args);
        builder.Build().Run();
    }
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}
Hanseatic answered 15/2, 2024 at 18:39 Comment(0)
S
1
public class Program
{
    public static async Task Main(string[] args)
    {
        var host = CreateHostBuilder(args).Build();
        await host.RunAsync();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .UseSerilog()
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>()
                          .UseWebRoot("wwwroot");
            });
}

This is the startup class

public class Startup
   {
       public IConfiguration Configuration { get; }

 public Startup(IConfiguration configuration)
   {
       Configuration = configuration;
   }

   public void ConfigureServices(IServiceCollection services)
   {
       // add your services here
   }

   public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
   {
       //add configuration/middleware here

   }
 
  }
Stith answered 20/4, 2024 at 10:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.