How to migrate ASP.NET core 5 code to ASP.NET core 6

It is easy to convert your ASP.NET Core 5 code into ASP.NET Core 6. These examples will show you how.

Microsoft’s ASP.NET Core 6 is now available for production. It introduces a simplified hosting system that reduces the amount of boilerplate code you need to get your ASP.NET Core app up and running. ASP.NET Core 6 makes it easier to create a web application from scratch than ASP.NET Core 5.

What if you need to upgrade an ASP.NET Core 5 Project to ASP.NET Core 6 code? If you need to update ASP.NET Core 5 code, you must be familiar with the code required to convert ASP.NET Core 5 code into ASP.NET Core 6. This article contains several examples of code that will help you do this.

Visual Studio 2022 is required to use the code examples in this article.

Visual Studio 2022 allows you to create an ASP.NET Core Web API Project.

Let’s first create an ASP.NET Core Project in Visual Studio 2022. These steps will allow you to create a new ASP.NET Core Web API 6 Project in Visual Studio 2022.

  1. Start the Visual Studio 2022 IDEA.
  2. Click on “Create a new project.”
  3. Select “ASP.NET Core Web API” in the “Create a new project” window.
  4. Click Next.
  5. In the “Configure a new project” window, enter the name and the location of the project.
  6. Depending on your preferences, you can also check the box “Place solution or project in the same directories”
  7. Click Next.
  8. Next, make sure that you check the box that says “Use …” controllers” in the “Additional Information”. We’ll be using controllers rather than minimal APIs in this example. Set the “Authentication Type” to “None” (default).
  9. As we will not be using these features, ensure that you have unchecked the boxes “Enable Docker”, “Configure For HTTPS” and “Enable OpenAPI Support”.
  10. Click on Create.

This ASP.NET Core 6 Web API Project will be used to demonstrate migrations from ASP.NET Core 5 to ASP.NET Core 6 code in the following sections.

Read also: Web Application Testing Checklist

The Program class in the ASP.NET Core 5

This code snippet shows you how a typical Program class looks in ASP.NET Core 5.

public class Program { public static void Main(string[] args)  CreateHostBuilder(args).Build().Run();  public static IHostBuilder CreateHostBuilder(string[] args) { return Host.CreateDefaultBuilder(args). ConfigureWebHostDefaults(x => x.UseStartup  ()); } }

The Program class in Apache.NET Core 6

The simplified hosting model was introduced in ASP.NET Core 6. You no longer need to use Startup classes. This is covered in my earlier article. Here is how to create a program class in ASP.NET Core 6.

var builder = WebApplication.CreateBuilder(args); // Add services to the container builder.Services.AddControllers(); var app = builder.Build(); // Configure the HTTP request pipeline app.UseAuthorization(); app.MapControllers(); app.Run();

Add middleware to ASP.NET Core 5

This code snippet demonstrates how to add a middleware component to ASP.NET Core 5. In this example, we will add the middleware.

public class Startup  public void Configure(IApplicationBuilder app)  app.UseResponseCompression();  

Add middleware to ASP.NET Core 6

The following code can be used to add a middleware component to ASP.NET Core 6.

var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); app.UseResponseCompression(); app.Run();

Add routing to ASP.NET Core 5

The following code can be used to add an endpoint to ASP.NET Core 5.

public class Startup { public void Configure(IApplicationBuilder app) { app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapGet("/test", () => "This is a test message. "); }); } }

Add routing to ASP.NET Core 6.

The following code can be used to add an endpoint to ASP.NET Core 6.

var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); app.MapGet("/test", () => "This is a test message. "); app.Run();

You can add endpoints directly to WebApplication in ASP.NET Core 6 without making explicit calls to the UseRouting and UseEndpoints extension method.

Add services to ASP.NET Core 5

This code snippet shows you how to add services to the container using ASP.NET Core 5.

public class Startup { public void ConfigureServices(IServiceCollection services) { // Add built-in services services.AddMemoryCache(); services.AddRazorPages(); services.AddControllersWithViews(); // Add a custom service services.AddScoped(); } }

Add services to ASP.NET Core 6

The following code can be used to add services to the container using ASP.NET Core 6.

var builder = WebApplication.CreateBuilder(args); // Add built-in services builder.Services.AddMemoryCache(); builder.Services.AddRazorPages(); builder.Services.AddControllersWithViews(); // Add a custom service builder.Services.AddScoped(); var app = builder.Build();

Test an ASP.NET Core 6 or 5 application

You can test an ASP.NET Core 5 application using either TestServer or WebApplicationFactory. The following code snippet will allow you to test ASP.NET Core 5 using TestServer

[Fact] public async Task GetProductsTest() { using var host = Host.CreateDefaultBuilder() .ConfigureWebHostDefaults(builder => { builder.UseTestServer() .UseStartup(); }) .ConfigureServices(services => { services.AddSingleton(); }) .Build(); await host.StartAsync(); var client = host.GetTestClient(); var response = await client.GetStringAsync("/getproducts"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); }

The following code snippet shows how you can test your ASP.NET Core 5 application using WebApplicationFactory.

[Fact] public async Task GetProductsTest() { var application = new WebApplicationFactory() .WithWebHostBuilder(builder => { builder.ConfigureServices(services => { services.AddSingleton(); }); }); var client = application.CreateClient(); var response = await client.GetStringAsync("/getproducts"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); }

You can use the same code to test using TestServer or WebApplicationFactory in .NET 5 and .NET 6.

Add a logging provider to ASP.NET Core 5

Logging providers are used in ASP.NET Core to store logs. The default logging providers in ASP.NET Core include the EventLog, Console, EventLog and EventSource logging provider.

ClearProviders can be used to remove all logging providers, add a specific provider, or create your own custom log provider. This code snippet shows how to remove all ILoggerProvider instance and add the Console logging service in ASP.NET Core 5.

public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.ClearProviders(); logging.AddConsole(); ) .ConfigureWebHostDefaults(webBuilder =>{ webBuilder.UseStartup(); });

Add a logging provider to ASP.NET Core 6

In ASP.NET Core 6, when you call WebApplication.CreateBuilder, it adds the Console, Debug, EventLog, and EventSource logging providers. This code snippet demonstrates how to clear default logging providers in ASP.NET Core 6 and add the Console logging provider.

var builder = WebApplication.CreateBuilder(args); //Clear default logging providers builder.Logging.ClearProviders(); //Code to add services to the container builder.Logging.AddConsole(); var app = builder.Build();

These code examples show how middleware, routing and services can be added in ASP.NET Core 5 or 6. There are also differences in the Program class, testing, and programming. These snippets will help you with ASP.NET Core 6 applications and give you a head start when migrating your ASP.NET Core 5 apps to ASP.NET Core 6.