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

Microsoft’s ASP.Internet Main 6, which has been readily available for manufacturing use because November 8, introduces a simplified web hosting design that lessens the boilerplate code that you would if not want to produce to get your ASP.Net Core application up and managing. ASP.Internet Main 6 makes a bit less difficult to create a new website software from scratch, when compared with ASP.Internet Main 5.

But what if you want to update an ASP.Web Main 5 job to ASP.Net Core 6? In that case, you really should be conscious of the code you will will need to write to migrate ASP.Internet Main 5 code to ASP.Internet Main 6. This post offers many code samples that demonstrate how you can do this.

To do the job with the code examples delivered in this post, you need to have Visible Studio 2022 installed in your program. If you do not currently have a duplicate, you can download Visible Studio 2022 in this article.

Build an ASP.Net Main Website API challenge in Visible Studio 2022

Initially off, let us generate an ASP.Internet Main task in Visible Studio 2022. Next these measures will build a new ASP.Internet Core Website API 6 task in Visual Studio 2022:

  1. Start the Visible Studio 2022 IDE.
  2. Click on on “Create new undertaking.”
  3. In the “Create new project” window, find “ASP.Internet Main World-wide-web API” from the list of templates exhibited.
  4. Click on Future.
  5. In the “Configure your new project” window, specify the identify and place for the new challenge.
  6. Optionally look at the “Place answer and undertaking in the exact directory” examine box, based on your choices.
  7. Click Up coming.
  8. In the “Additional Information” window shown future, make certain that the verify box that suggests “Use controllers…” is checked, as we’ll be working with controllers as an alternative of negligible APIs in this case in point. Go away the “Authentication Type” established to “None” (default).
  9. Guarantee that the check out containers “Enable Docker,” “Configure for HTTPS,” and “Enable Open up API Support” are unchecked as we won’t be utilizing any of individuals options here.
  10. Click Build.

We’ll use this ASP.Net Core 6 Net API undertaking to illustrate migrations of ASP.Internet Core 5 code to ASP.Web Main 6 in the subsequent sections of this short article.

The Method course in ASP.Internet Core 5

The next code snippet illustrates what a common System course appears like in ASP.Net Main 5.

general public class System

      community static void Primary(string[] args)
      public static IHostBuilder CreateHostBuilder(string[] args)
            return Host.CreateDefaultBuilder(args).
            ConfigureWebHostDefaults(x => x.UseStartup ())

The Program course in ASP.Web Main 6

With the introduction of the simplified web hosting product in ASP.Internet Core 6, you no for a longer period have to use the Startup course. You can examine much more about this in my before article listed here. Here’s how you would publish a standard Software course in ASP.Internet Main 6:

var builder = WebApplication.CreateBuilder(args)
// Insert expert services to the container
builder.Expert services.AddControllers()
var app = builder.Create()
// Configure the HTTP ask for pipeline

Incorporate middleware in ASP.Net Main 5

The adhering to code snippet reveals how you can incorporate a middleware ingredient in ASP.Net Core 5. In our instance, we’ll add the response compression middleware.

community course Startup

    community void Configure(IApplicationBuilder app)

Insert middleware in ASP.Web Core 6

To include a middleware element in ASP.Web Main 6, you can use the subsequent code.

var builder = WebApplication.CreateBuilder(args)
var application = builder.Develop()

Incorporate routing in ASP.Net Main 5

To incorporate an endpoint in ASP.Web Core 5, you can use the next code.

general public class Startup

    community void Configure(IApplicationBuilder application)
        application.UseEndpoints(endpoints =>
            endpoints.MapGet("/take a look at", () => "This is a exam information.")

Insert routing in ASP.Web Core 6

You can add an endpoint in ASP.Internet Core 6 working with the adhering to code.

var builder = WebApplication.CreateBuilder(args)
var app = builder.Create()
application.MapGet("/check", () => "This is a take a look at message.")

Note that in ASP.Internet Main 6 you can incorporate endpoints to WebApplication without having acquiring to make explicit phone calls to the UseRouting or UseEndpoints extension solutions.

Include expert services in ASP.Net Core 5

The following code snippet illustrates how you can incorporate solutions to the container in ASP.Web Core 5.

community class Startup

    general public void ConfigureServices(IServiceCollection services)
        // Increase built-in products and services
        // Include a customized service

Incorporate companies in ASP.Internet Main 6

To increase products and services to the container in ASP.Internet Core 6, you can use the subsequent code.

var builder = WebApplication.CreateBuilder(args)
// Increase crafted-in companies
// Include a custom made services
builder.Products and services.AddScoped()
var application = builder.Establish()

Check an ASP.Internet Main 5 or ASP.Net Core 6 software

You can examination an ASP.Web Core 5 software utilizing both TestServer or WebApplicationFactory. To test making use of TestServer in ASP.Internet Core 5, you can use the subsequent code snippet.

public async Undertaking GetProductsTest()

    making use of var host = Host.CreateDefaultBuilder()
        .ConfigureWebHostDefaults(builder =>
        .ConfigureServices(providers =>
            expert services.AddSingleton()
    await host.StartAsync()
    var shopper = host.GetTestClient()
    var response = await client.GetStringAsync("/getproducts")
    Assert.Equal(HttpStatusCode.Ok, response.StatusCode)

The pursuing code snippet reveals how you can take a look at your ASP.Net Core 5 application using WebApplicationFactory.

community async Undertaking GetProductsTest()

    var software = new WebApplicationFactory()
        .WithWebHostBuilder(builder =>
            builder.ConfigureServices(companies =>
    var customer = software.CreateClient()
    var reaction = await consumer.GetStringAsync("/getproducts")
    Assert.Equivalent(HttpStatusCode.Okay, reaction.StatusCode)

You can use the exact same code to check utilizing TestServer or WebApplicationFactory in .Net 5 and .Net 6. 

Increase a logging provider in ASP.Internet Core 5

Logging suppliers in ASP.Internet Main are utilised to keep logs. The default logging companies included in ASP.Web Core are the Debug, Console, EventLog, and EventSource logging providers.

You can use the ClearProviders process to obvious all logging companies and insert a unique logging provider or your individual personalized logging supplier. The following code snippet illustrates how you can get rid of all ILoggerProvider instances and include the Console logging service provider in ASP.Web Main 5.

general public static IHostBuilder CreateHostBuilder(string[] args) =>
      .ConfigureLogging(logging =>
      .ConfigureWebHostDefaults(webBuilder =>

Incorporate a logging supplier in ASP.Net Core 6

In ASP.Internet Core 6, when you simply call WebApplication.CreateBuilder, it adds the Console, Debug, EventLog, and EventSource logging providers. The adhering to code snippet reveals how you can obvious the default logging providers and increase only the Console logging provider in ASP.Net Main 6.

var builder = WebApplication.CreateBuilder(args)
//Clear default logging suppliers
//Code to increase products and services to the container
var application = builder.Develop()

The code illustrations offered here illustrate the unique ways we add middleware, routing, products and services, and logging companies in ASP.Internet Main 5 and in ASP.Web Main 6, as nicely as distinctions in the Method class and tests. These snippets ought to assist you when performing with ASP.Internet Main 6 apps, and get you off to a good start when you migrate your ASP.Net Main 5 applications to ASP.Web Main 6.

Copyright © 2022 IDG Communications, Inc.