This also applies to .NET 6.0 ASP.NET Core app:
.NET 6 C# console app template generates top-level statements
https://learn.microsoft.com/en-us/dotnet/core/tutorials/top-level-templates
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
I noticed, that new "MS VS 2022 ASP .NET core WebApp" has modified the "Program.cs" file:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();
there are no namespaces, no methods, no classes ... how can I go back to old style and display all missing code ???
Thanks
Jerry
This also applies to .NET 6.0 ASP.NET Core app:
.NET 6 C# console app template generates top-level statements
https://learn.microsoft.com/en-us/dotnet/core/tutorials/top-level-templates
Yes, C# WPF .NET 6 generates only top-level statements.
ASP C# .NET 6 generates also only top-level statements.
The biggest problem is that there is also missing "startup.cs" file.
As you can see below - the older version vs the newer one .... how can I display all missing lines and files in the
newer version ? Where they are ?
startup was just two methods called by the app builder.
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
a method to register services and a method to add middleware. these are just done inline. as endpoint routing is default, no need to configure. you can add it back, but why? startup was just to make it look like the old asp.net
var builder = WebApplication.CreateBuilder(args);
// register services here old - startup.Configuration()
builder.Services.AddScoped<IMyService, MyService>();
var app = builder.Build();
// register middleware - old startup.Configure()
app.UseStaticFiles();
app.MapGet("/", () => "Hello World!");
app.Run();
The following Microsoft document might be helpful. See the sections "New hosting model" and below.
Migrate from ASP.NET Core 5.0 to 6.0
https://learn.microsoft.com/en-us/aspnet/core/migration/50-to-60?view=aspnetcore-6.0&tabs=visual-studio
Hi @JerryM ,
For example, we create a Asp.net Core Web App (MVC, Asp.Net 6), and the program.cs like this:
To add back the Startup Class to ASP.NET Core 6, we can right click the project and add a Startup.cs file, and add its content based on the Program.cs file content. Like this:
You can view the source code from here:
After running the application, it works well:
More detail information about the code samples migrating from Asp.net 5 to Asp.net 6, see Code samples migrated to the new minimal hosting model in ASP.NET Core 6.0.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion