Best practice for using Program.cs vs Startup.cs in .NET 8/10 for new and migrated projects

Soumyadip Majumder 245 Reputation points

I am working with ASP.NET Core and have some confusion regarding project structure in recent .NET versions.

In earlier versions (before .NET 6), the project typically used both Startup.cs and Program.cs, where Startup.cs handled service configuration and middleware setup. However, starting from .NET 6 and later, the minimal hosting model allows all configuration to be handled within Program.cs.

I would like guidance on current best practices from a development and engineering perspective:

  • For a new project built from scratch in .NET 8 or later, should we use only Program.cs, or is there any benefit in still maintaining a separate Startup.cs file?
  • For migrating a legacy project that already uses both Startup.cs and Program.cs, is it recommended to consolidate everything into Program.cs, or is it acceptable to retain both files?
  • Are there any considerations related to maintainability, scalability, or team collaboration that should influence this decision?

I am looking for recommendations aligned with modern ASP.NET Core practices.

0 comments No comments

Sign in to comment

Answer accepted by question author

Jack Dang (WICLOUD CORPORATION) 18,970 Reputation points β€’ Microsoft External Staff β€’ Moderator

Hi @Soumyadip Majumder ,

Thanks for reaching out.

For a new ASP.NET Core project in .NET 8 or later, I would generally lean toward following the current template style and using Program.cs with the minimal hosting model. That is the direction the platform has taken since .NET 6, and for many new apps it tends to keep the startup flow simpler and easier to follow.

That said, using Program.cs does not mean everything has to live in one big file. In practice, it often works well to keep Program.cs as the composition root and move the detailed setup into extension methods or separate modules as the app grows. That usually gives you the benefits of the modern hosting model without necessarily ending up with a very large startup file.

For an existing project that already uses Startup.cs, I do not think there is usually a strong need to rewrite it just for the sake of matching the new style. Microsoft’s migration guidance is fairly clear that apps moving to .NET 6 or later do not have to adopt the minimal hosting model right away, and the older Startup pattern is still supported. If the current structure is clean, understandable, and working well, keeping it is often perfectly reasonable.

So for migrated projects, I would usually only consolidate into Program.cs if there is a real payoff, such as simplifying the startup path, aligning the project with current templates, or making onboarding easier for the team. Otherwise, a low-risk migration is often the better engineering choice than a structural rewrite that does not really change behavior.

If you want a middle ground, you can move to the modern hosting model and still keep a Startup-style class for a while by calling it explicitly from Program.cs, for example:

var builder = WebApplication.CreateBuilder(args);

var startup = new Startup(builder.Configuration);
startup.ConfigureServices(builder.Services);

var app = builder.Build();

startup.Configure(app, app.Environment);

app.Run();

The sample above is only for reference, so you may need to adjust it based on your application structure, dependency injection setup, middleware order, endpoint mapping, and any third-party containers or hosting customizations you use.

From a maintainability and team perspective, I would focus less on whether the file is named Startup.cs or Program.cs, and more on whether the startup path is easy to read and change safely. A small or medium app can often be perfectly fine with a single Program.cs. A larger app will usually benefit from extracting service registration and middleware setup into well-named extension methods or feature-level modules instead of letting Program.cs grow too much.

Hope this helps! If my explanation and the information I provided were helpful, I would greatly appreciate it if you could follow the instructions here so others with the same problem can benefit as well.

  1. Soumyadip Majumder 245 Reputation points

    @Jack Dang (WICLOUD CORPORATION)
    For practice purposes we can keep everything in program.cs file. But in real-time projects that contains multiple variants services, middleware; Should I practice with program.cs and startup.cs both the files? In .NET 8/10 we can keep startup.cs file that is optional.

  2. Jack Dang (WICLOUD CORPORATION) 18,970 Reputation points β€’ Microsoft External Staff β€’ Moderator

    Hi @Soumyadip Majumder ,

    I would focus on Program.cs, because that is the current default approach in .NET 8 and later.

    At the same time, it is still useful to understand Startup.cs, since many existing real-world projects still use that pattern. So my suggestion would be: practice building new applications with Program.cs, and learn Startup.cs well enough to read, maintain, or migrate older codebases when needed.

  3. Soumyadip Majumder 245 Reputation points

    I feel same, some legacy projects or the projects have not only multiple but also variants service configurations and middlewares, we can use startup.cs to seperate the lengthy blocks of code and keep the program.cs minimal. For new fresh scratch project, we should maintain only program.cs by default but based on business understanding we can use startup cs, that is optional.

  4. Jack Dang (WICLOUD CORPORATION) 18,970 Reputation points β€’ Microsoft External Staff β€’ Moderator

    Hi @Soumyadip Majumder ,

    Thanks for your feedback.

    If my explanation was helpful to you so far, I would greatly appreciate it if you could follow the instructions here so others with the same problem can benefit as well. Thank you.


Sign in to comment

0 additional answers

Sign in to answer

Your answer