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


Microsoft’s ASP.Internet Main 6, which has been out there for output use due to the fact November 8, introduces a simplified internet hosting model that cuts down the boilerplate code that you would usually need to compose to get your ASP.Web Main software up and functioning. ASP.Internet Core 6 helps make a bit easier to make a new web application from scratch, in contrast with ASP.Web Main 5.

But what if you want to update an ASP.Web Main 5 job to ASP.Internet Core 6? In that case, you should really be aware of the code you will need to create to migrate ASP.Net Main 5 code to ASP.Net Core 6. This posting provides several code samples that clearly show how you can do this.

To function with the code illustrations offered in this article, you must have Visual Studio 2022 installed in your technique. If you do not currently have a copy, you can download Visual Studio 2022 in this article.

Make an ASP.Internet Main World wide web API project in Visual Studio 2022

Initially off, let us build an ASP.Net Main job in Visible Studio 2022. Next these measures will make a new ASP.Web Main World wide web API 6 project in Visual Studio 2022:

  1. Launch the Visual Studio 2022 IDE.
  2. Click on on “Create new challenge.”
  3. In the “Create new project” window, decide on “ASP.Internet Core Web API” from the checklist of templates exhibited.
  4. Simply click Up coming.
  5. In the “Configure your new project” window, specify the title and site for the new project.
  6. Optionally check the “Place alternative and challenge in the exact directory” verify box, depending on your choices.
  7. Click on Up coming.
  8. In the “Additional Information” window revealed next, be certain that the check box that claims “Use controllers…” is checked, as we’ll be employing controllers rather of minimum APIs in this case in point. Go away the “Authentication Type” established to “None” (default).
  9. Assure that the verify packing containers “Enable Docker,” “Configure for HTTPS,” and “Enable Open API Support” are unchecked as we won’t be working with any of individuals capabilities here.
  10. Simply click Make.

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

The Software class in ASP.Web Main 5

The adhering to code snippet illustrates what a usual Method course appears to be like like in ASP.Internet Main 5.

general public course Application

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

The Application class in ASP.Internet Core 6

With the introduction of the simplified hosting product in ASP.Net Main 6, you no for a longer period have to use the Startup class. You can read through more about this in my earlier short article in this article. Here’s how you would publish a regular Application course in ASP.Net Core 6:

var builder = WebApplication.CreateBuilder(args)
// Incorporate expert services to the container
builder.Products and services.AddControllers()
var application = builder.Develop()
// Configure the HTTP ask for pipeline

Include middleware in ASP.Internet Main 5

The following code snippet exhibits how you can add a middleware element in ASP.Net Core 5. In our example, we’ll add the reaction compression middleware.

general public class Startup

    community void Configure(IApplicationBuilder application)

Incorporate middleware in ASP.Net Core 6

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

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

Incorporate routing in ASP.Net Core 5

To increase an endpoint in ASP.Net Main 5, you can use the subsequent code.

general public class Startup

    general public void Configure(IApplicationBuilder application)
        app.UseEndpoints(endpoints =>
            endpoints.MapGet("/exam", () => "This is a check message.")

Include routing in ASP.Net Core 6

You can incorporate an endpoint in ASP.Web Main 6 applying the following code.

var builder = WebApplication.CreateBuilder(args)
var app = builder.Create()
application.MapGet("/examination", () => "This is a examination information.")

Notice that in ASP.Internet Core 6 you can increase endpoints to WebApplication without having to make explicit phone calls to the UseRouting or UseEndpoints extension solutions.

Insert solutions in ASP.Internet Core 5

The adhering to code snippet illustrates how you can insert providers to the container in ASP.Net Main 5.

public class Startup

    general public void ConfigureServices(IServiceCollection solutions)
        // Include constructed-in services
        products and services.AddMemoryCache()
        // Incorporate a tailor made services

Increase companies in ASP.Internet Core 6

To insert expert services to the container in ASP.Net Main 6, you can use the adhering to code.

var builder = WebApplication.CreateBuilder(args)
// Insert constructed-in providers
// Add a custom made assistance
builder.Products and services.AddScoped()
var application = builder.Make()

Check an ASP.Web Core 5 or ASP.Net Main 6 application

You can check an ASP.Internet Core 5 application using both TestServer or WebApplicationFactory. To check utilizing TestServer in ASP.Net Main 5, you can use the following code snippet.

community async Process GetProductsTest()

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

The adhering to code snippet exhibits how you can check your ASP.Web Core 5 application employing WebApplicationFactory.

general public async Job GetProductsTest()

    var software = new WebApplicationFactory()
        .WithWebHostBuilder(builder =>
            builder.ConfigureServices(services =>
    var consumer = software.CreateClient()
    var response = await client.GetStringAsync("/getproducts")
    Assert.Equivalent(HttpStatusCode.Ok, response.StatusCode)

You can use the very same code to take a look at employing TestServer or WebApplicationFactory in .Net 5 and .Net 6. 

Incorporate a logging supplier in ASP.Internet Main 5

Logging suppliers in ASP.Net Core are employed to retail store logs. The default logging companies involved in ASP.Net Main are the Debug, Console, EventLog, and EventSource logging companies.

You can use the ClearProviders process to crystal clear all logging providers and add a specific logging supplier or your very own custom made logging provider. The next code snippet illustrates how you can remove all ILoggerProvider occasions and insert the Console logging provider in ASP.Net Core 5.

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

Add a logging company in ASP.Web Main 6

In ASP.Net Main 6, when you contact WebApplication.CreateBuilder, it adds the Console, Debug, EventLog, and EventSource logging suppliers. The following code snippet reveals how you can apparent the default logging vendors and increase only the Console logging service provider in ASP.Internet Main 6.

var builder = WebApplication.CreateBuilder(args)
//Apparent default logging suppliers
//Code to incorporate companies to the container
var app = builder.Make()

The code examples furnished in this article illustrate the various means we add middleware, routing, solutions, and logging vendors in ASP.Internet Main 5 and in ASP.Web Main 6, as well as variations in the System course and testing. These snippets ought to assist you when doing the job with ASP.Web Core 6 apps, and get you off to a very good get started when you migrate your ASP.Internet Core 5 apps to ASP.Internet Main 6.

Copyright © 2022 IDG Communications, Inc.


Source backlink