ASP.NET Core 6.0 - Top-Level Statements
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.
- ASP.NET Core 6.0 - Users With Device 2FA
- ASP.NET Core 6.0 - Migrate From .NET 5.0
- ASP.NET Core 6.0 - Top-Level Statements
- ASP.NET Core 6.0 - Global Usings
- ASP.NET Core 6.0 - Data Protection Keys
- ASP.NET Core 6.0 - Administrator Claim
- ASP.NET Core 6.0 - Terms Of Service
- ASP.NET Core 6.0 - Claims-Based Authorization
- ASP.NET Core 6.0 - Impersonate User
- ASP.NET Core 6.0 - Remember Me Or Not
- ASP.NET Core 6.0 - Zero Trust
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.
Program.cs:
var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorPages(); var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapRazorPages(); app.Run();
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. app.Run();
Step 5:
Delete Startup.cs.
Step 6:
Resolve usings namespaces. See ASP.NET Core 6.0 - Global Usings.
Comments(0)