ASP.NET Core 6.0 - Top-Level Statements

Ken Haggerty
Created 01/05/2022 - Updated 01/12/2022 02:33 GMT

This article describes the new top-level statements and how to migrate Startup.cs code to Program.cs. I will assume you have downloaded the ASP.NET Core 6.0 - Users With Device 2FA Project.

Users With Device 2FA Project and Article Series

This article series is about the implementation of ASP.NET Core 6.0 with Visual Studio 2022. The ASP.NET Core 6.0 - Users With Device 2FA Project (UWD2FAP) implements WebAuthn, also known as FIDO2, instead of authenticator apps for two-factor authentication (2FA). The project implements Bootstrap v5 and Bootstrap Native. After a user registers, they can enable 2FA with Windows Hello, Android Lock Screen, or a FIDO2 security key. If I had this project when I created KenHaggerty. Com three years ago, I would have started with this user authentication template. The latest version is published at Preview. KenHaggerty. Com. I encourage you to register and evaluate multiple 2FA devices in Manage Account > Two-Factor Authentication. Details, screenshots, and related articles can be found at ASP.NET Core 6.0 - Users With Device 2FA Project. The details page includes the version change log.

Updated with links as the articles become published.

Visual Studio 2022 (VS 2022) is required to develop .NET 6 and ASP.NET Core 6.0 applications. VS 2022 Downloads.

Top-level statements means the compiler generates the namespace, class, and method elements for your main program. You can look at the code for the new application and imagine that it contains the statements inside the Main method generated by earlier templates. You can add more statements to the program, just like you can add more statements to your Main method in the traditional style. You can even add functions. They're created as local functions nested inside the generated Main method. See MSDocs - New C# templates generate top-level statements.

Starting in C# 9, you don't have to explicitly include a Main method in a console application project. Instead, you can use the top-level statements feature to minimize the code you have to write. In this case, the compiler generates a class and Main method entry point for the application. See MSDocs - Top-level statements - programs without Main methods.

When you create a new ASP.NET Core 6.0 Web App with VS 2022, the template generates the new top-level Program.cs which includes the code formerly implemented with Startup.cs.

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.

var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see



Here are 6 steps to implement the top-level statements feature after you migrate an existing ASP.NET Core 5.0 Web App. See ASP.NET Core 6.0 - Migrate From .NET 5.0.

Step 1:

Replace all code in Program.cs with the following.

using Microsoft.AspNetCore.Builder;

// Configure Services with the WebApplicationBuilder
var builder = WebApplication.CreateBuilder(args);

// Step 2: Copy and paste code from the Startup > ConfigureServices method here.
// Step 3: Replace the services parameter with builder.Services (WebApplicationBuilder.IServiceCollection).

var app = builder.Build();
// Configure the HTTP request pipeline.

// Step 4: Copy and paste code from the Startup > Configure method here.

Step 5:

Delete Startup.cs.

Step 6:

Resolve usings namespaces. See ASP.NET Core 6.0 - Global Usings.

IConfiguration Configuration is implemented with builder. Configuration.
IWebHostEnvironment env is implemented with builder. Environment.

Article Tags:

VS 2022

Comment Count = 0

Please log in to comment.

Login Register
Logged in users receive web notifications.
Web Notifications