2

I'm new to ASP.NET Core. I'm using ASP.NET Core 7. I read that in ASP.NET Core 7 they got rid of Startup.cs. Can someone show me how to merge Startup.cs into Program.cs in this example.

https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/master/2-WebApp-graph-user/2-1-Call-MSGraph

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
joegreen
  • 59
  • 4
  • "I read that in asp.net core 7 they got rid of Startup.cs" nope. https://learn.microsoft.com/en-us/aspnet/core/migration/50-to-60?view=aspnetcore-7.0&tabs=visual-studio#faq – gunr2171 Aug 08 '23 at 11:39
  • That's good to know. But they say The new hosting model is the preferred way to host new apps using .NET 6 and later. As I'm learning, I want to know how to move code to Program.cs from Startup.cs. – joegreen Aug 08 '23 at 11:46
  • 1
    NOT a dupe but related https://stackoverflow.com/q/74691624/125981 – Mark Schultheiss Aug 08 '23 at 11:53

1 Answers1

4

I read that in ASP.NET Core 7 they got rid of Startup.cs.

Not exactly. ASP.NET Core 6 introduced a new so called minimal hosting model (which is used by the default template). But you are free to keep the generic hosting model if you want.

How to merge Startup.cs into Program.cs

In short - move everything from ConfigureServices to before var app = builder.Build(); call and everything from Configure - after it (check out code generated by the default template). To setup services you can use builder.Services, cofiguration is accessible from both builder and app via correspondingly named property and use app to perform the calls from Startup.Configure.

See also:

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • Where should I put a function like GetSecretFromKeyVault? Can I include functions like these in Program.cs? – joegreen Aug 08 '23 at 12:06
  • @joegreen you can put it in your `Program.cs` file. It will become a [local function](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions) so it should be placed before any class declarations if any there (in the Program.cs). – Guru Stron Aug 08 '23 at 12:08