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


Microsoft’s ASP.Web Core 6, which has been offered for manufacturing use considering the fact that November 8, introduces a simplified internet hosting model that lessens the boilerplate code that you would in any other case will need to produce to get your ASP.Internet Main software up and operating. ASP.Net Main 6 helps make a little bit easier to make a new web application from scratch, when compared with ASP.Net Main 5.

But what if you want to update an ASP.Web Core 5 challenge to ASP.Web Main 6? In that situation, you need to be knowledgeable of the code you will need to write to migrate ASP.Web Core 5 code to ASP.Web Main 6. This posting offers several code samples that present how you can do this.

To function with the code illustrations presented in this article, you should really have Visible Studio 2022 installed in your program. If you do not previously have a duplicate, you can obtain Visible Studio 2022 right here.

Make an ASP.Internet Core World-wide-web API venture in Visible Studio 2022

First off, let’s develop an ASP.Web Main undertaking in Visible Studio 2022. Adhering to these steps will produce a new ASP.Internet Core World-wide-web API 6 job in Visible Studio 2022:

  1. Start the Visual Studio 2022 IDE.
  2. Click on “Create new undertaking.”
  3. In the “Create new project” window, pick “ASP.Internet Core World wide web API” from the checklist of templates exhibited.
  4. Simply click Subsequent.
  5. In the “Configure your new project” window, specify the title and place for the new project.
  6. Optionally check out the “Place remedy and venture in the very same directory” look at box, depending on your preferences.
  7. Click on Following.
  8. In the “Additional Information” window demonstrated future, ensure that the test box that states “Use controllers…” is checked, as we’ll be making use of controllers as a substitute of small APIs in this instance. Go away the “Authentication Type” set to “None” (default).
  9. Be certain that the examine boxes “Enable Docker,” “Configure for HTTPS,” and “Enable Open up API Support” are unchecked as we will not be applying any of these attributes below.
  10. Click Produce.

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

The Plan class in ASP.Web Main 5

The pursuing code snippet illustrates what a typical Application course seems to be like in ASP.Internet Core 5.

community class System

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

The Method class in ASP.Internet Core 6

With the introduction of the simplified hosting model in ASP.Net Core 6, you no longer have to use the Startup course. You can study a lot more about this in my earlier article in this article. Here’s how you would generate a standard Software course in ASP.Web Main 6:

var builder = WebApplication.CreateBuilder(args)
// Increase expert services to the container
var application = builder.Create()
// Configure the HTTP ask for pipeline

Incorporate middleware in ASP.Internet Core 5

The adhering to code snippet displays how you can increase a middleware element in ASP.Web Main 5. In our case in point, we’ll include the response compression middleware.

public course Startup

    community void Configure(IApplicationBuilder application)

Add middleware in ASP.Net Main 6

To insert a middleware element in ASP.Net Main 6, you can use the next code.

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

Incorporate routing in ASP.Net Core 5

To incorporate an endpoint in ASP.Net Main 5, you can use the adhering to code.

public course Startup

    public void Configure(IApplicationBuilder app)
        app.UseEndpoints(endpoints =>
            endpoints.MapGet("/test", () => "This is a examination message.")

Increase routing in ASP.Web Core 6

You can include an endpoint in ASP.Web Main 6 working with the next code.

var builder = WebApplication.CreateBuilder(args)
var app = builder.Establish()
application.MapGet("/examination", () => "This is a take a look at concept.")

Take note that in ASP.Web Core 6 you can incorporate endpoints to WebApplication devoid of obtaining to make specific calls to the UseRouting or UseEndpoints extension procedures.

Increase services in ASP.Internet Core 5

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

community course Startup

    community void ConfigureServices(IServiceCollection services)
        // Add built-in providers
        // Include a tailor made provider

Increase providers in ASP.Net Main 6

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

var builder = WebApplication.CreateBuilder(args)
// Increase designed-in solutions
builder.Products and services.AddControllersWithViews()
// Incorporate a personalized service
var app = builder.Develop()

Take a look at an ASP.Net Core 5 or ASP.Internet Main 6 software

You can take a look at an ASP.Web Main 5 software utilizing possibly TestServer or WebApplicationFactory. To examination employing TestServer in ASP.Net Main 5, you can use the next code snippet.

public async Job GetProductsTest()

    employing var host = Host.CreateDefaultBuilder()
        .ConfigureWebHostDefaults(builder =>
        .ConfigureServices(expert services =>
    await host.StartAsync()
    var shopper = host.GetTestClient()
    var reaction = await shopper.GetStringAsync("/getproducts")
    Assert.Equal(HttpStatusCode.Alright, reaction.StatusCode)

The next code snippet demonstrates how you can take a look at your ASP.Internet Core 5 application utilizing WebApplicationFactory.

general public async Activity GetProductsTest()

    var application = new WebApplicationFactory()
        .WithWebHostBuilder(builder =>
            builder.ConfigureServices(solutions =>
    var client = software.CreateClient()
    var response = await customer.GetStringAsync("/getproducts")
    Assert.Equivalent(HttpStatusCode.Alright, response.StatusCode)

You can use the exact code to examination making use of TestServer or WebApplicationFactory in .Web 5 and .Internet 6. 

Incorporate a logging supplier in ASP.Internet Core 5

Logging companies in ASP.Web Main are employed to retailer logs. The default logging vendors involved in ASP.Internet Core are the Debug, Console, EventLog, and EventSource logging suppliers.

You can use the ClearProviders technique to distinct all logging companies and add a particular logging company or your possess custom made logging company. The subsequent code snippet illustrates how you can clear away all ILoggerProvider circumstances and increase the Console logging provider in ASP.Web Main 5.

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

Insert a logging provider in ASP.Net Main 6

In ASP.Net Core 6, when you contact WebApplication.CreateBuilder, it provides the Console, Debug, EventLog, and EventSource logging companies. The pursuing code snippet displays how you can very clear the default logging providers and include only the Console logging supplier in ASP.Web Core 6.

var builder = WebApplication.CreateBuilder(args)
//Apparent default logging providers
//Code to increase expert services to the container
var application = builder.Build()

The code illustrations offered right here illustrate the distinct techniques we increase middleware, routing, services, and logging suppliers in ASP.Net Core 5 and in ASP.Net Core 6, as properly as dissimilarities in the System class and tests. These snippets need to assistance you when functioning with ASP.Internet Main 6 apps, and get you off to a superior commence when you migrate your ASP.Web Core 5 applications to ASP.Web Core 6.

Copyright © 2022 IDG Communications, Inc.


Source url