• Home
  • Insight
  • Blog
  • Business
  • Entertainment
  • Health
  • Politics
  • Shop
    • Gift Shop
    • Value Shop
    • Store
    • Bargain Shop
    • Discount
  • Sports
  • Tech
  • Travel
  • USA
  • Video
  • World
    • Asia
    • Africa
    • South America
    • North America
    • Europe
    • Oceania
Tuesday, November 17, 2026
No Result
View All Result
Subscribe Now
  • Home
  • Insight
  • Blog
  • Business
  • Entertainment
  • Health
  • Politics
  • Shop
    • Gift Shop
    • Value Shop
    • Store
    • Bargain Shop
    • Discount
  • Sports
  • Tech
  • Travel
  • USA
    Stranded in Conflict: Palestinians Navigate the Tenuous Balance of War and Peace Amid Ceasefire Efforts

    Stranded in Conflict: Palestinians Navigate the Tenuous Balance of War and Peace Amid Ceasefire Efforts

    Trump Administration Revokes Pay Increase for Head Start Educators

    Trump Administration Revokes Pay Increase for Head Start Educators

    Unexpected Triumph: Gay Candidates Achieve Unforeseen Victories in Challenging Races

    Unexpected Triumph: Gay Candidates Achieve Unforeseen Victories in Challenging Races

    Microsoft Outlook Experiences Prolonged Outage Affecting Users Nationwide

    Microsoft Outlook Experiences Prolonged Outage Affecting Users Nationwide

    Gunfire Erupts in D.C.: Authorities Search for Suspect Amid Rising Concerns Over Public Safety

    Gunfire Erupts in D.C.: Authorities Search for Suspect Amid Rising Concerns Over Public Safety

    LAUSD Takes Strong Action Against Hate Speech Following Racial Slur Incidents

    LAUSD Takes Strong Action Against Hate Speech Following Racial Slur Incidents

    Beloved Former Los Angeles Times Columnist Steve Harvey Passes Away at 80

    Beloved Former Los Angeles Times Columnist Steve Harvey Passes Away at 80

    Exploring Diversity: ‘Ten Lives’ Showcases the Varied Economic Journeys of Americans

    Exploring Diversity: ‘Ten Lives’ Showcases the Varied Economic Journeys of Americans

    Philadelphia Eagles Facing Turmoil as Coach Nick Sirianni Addresses Controversy Over Gesture to Fans

    Philadelphia Eagles Facing Turmoil as Coach Nick Sirianni Addresses Controversy Over Gesture to Fans

  • Video
  • World
    • Asia
    • Africa
    • South America
    • North America
    • Europe
    • Oceania
The Insight Post
  • Home
  • Insight
  • Blog
  • Business
  • Entertainment
  • Health
  • Politics
  • Shop
    • Gift Shop
    • Value Shop
    • Store
    • Bargain Shop
    • Discount
  • Sports
  • Tech
  • Travel
  • USA
    Stranded in Conflict: Palestinians Navigate the Tenuous Balance of War and Peace Amid Ceasefire Efforts

    Stranded in Conflict: Palestinians Navigate the Tenuous Balance of War and Peace Amid Ceasefire Efforts

    Trump Administration Revokes Pay Increase for Head Start Educators

    Trump Administration Revokes Pay Increase for Head Start Educators

    Unexpected Triumph: Gay Candidates Achieve Unforeseen Victories in Challenging Races

    Unexpected Triumph: Gay Candidates Achieve Unforeseen Victories in Challenging Races

    Microsoft Outlook Experiences Prolonged Outage Affecting Users Nationwide

    Microsoft Outlook Experiences Prolonged Outage Affecting Users Nationwide

    Gunfire Erupts in D.C.: Authorities Search for Suspect Amid Rising Concerns Over Public Safety

    Gunfire Erupts in D.C.: Authorities Search for Suspect Amid Rising Concerns Over Public Safety

    LAUSD Takes Strong Action Against Hate Speech Following Racial Slur Incidents

    LAUSD Takes Strong Action Against Hate Speech Following Racial Slur Incidents

    Beloved Former Los Angeles Times Columnist Steve Harvey Passes Away at 80

    Beloved Former Los Angeles Times Columnist Steve Harvey Passes Away at 80

    Exploring Diversity: ‘Ten Lives’ Showcases the Varied Economic Journeys of Americans

    Exploring Diversity: ‘Ten Lives’ Showcases the Varied Economic Journeys of Americans

    Philadelphia Eagles Facing Turmoil as Coach Nick Sirianni Addresses Controversy Over Gesture to Fans

    Philadelphia Eagles Facing Turmoil as Coach Nick Sirianni Addresses Controversy Over Gesture to Fans

  • Video
  • World
    • Asia
    • Africa
    • South America
    • North America
    • Europe
    • Oceania
No Result
View All Result
No Result
View All Result
Home Tech

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

by Theinsightpost
July 8, 2022
in Tech
0 0
0
How to migrate ASP.NET Core 5 code to ASP.NET Core 6


Microsoft’s ASP.NET Core 6, which has been available for production use since November 8, introduces a simplified hosting model that reduces the boilerplate code that you would otherwise need to write to get your ASP.NET Core application up and running. ASP.NET Core 6 makes a bit easier to create a new web application from scratch, compared with ASP.NET Core 5.

But what if you want to update an ASP.NET Core 5 project to ASP.NET Core 6? In that case, you should be aware of the code you will need to write to migrate ASP.NET Core 5 code to ASP.NET Core 6. This article presents several code samples that show how you can do this.

To work with the code examples provided in this article, you should have Visual Studio 2022 installed in your system. If you don’t already have a copy, you can download Visual Studio 2022 here.

Create an ASP.NET Core Web API project in Visual Studio 2022

First off, let’s create an ASP.NET Core project in Visual Studio 2022. Following these steps will create a new ASP.NET Core Web API 6 project in Visual Studio 2022:

  1. Launch the Visual Studio 2022 IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “ASP.NET Core Web API” from the list of templates displayed.
  4. Click Next.
  5. In the “Configure your new project” window, specify the name and location for the new project.
  6. Optionally check the “Place solution and project in the same directory” check box, depending on your preferences.
  7. Click Next.
  8. In the “Additional Information” window shown next, ensure that the check box that says “Use controllers…” is checked, as we’ll be using controllers instead of minimal APIs in this example. Leave the “Authentication Type” set to “None” (default).
  9. Ensure that the check boxes “Enable Docker,” “Configure for HTTPS,” and “Enable Open API Support” are unchecked as we won’t be using any of those features here.
  10. Click Create.

We’ll use this ASP.NET Core 6 Web API project to illustrate migrations of ASP.NET Core 5 code to ASP.NET Core 6 in the subsequent sections of this article.

The Program class in ASP.NET Core 5

The following code snippet illustrates what a typical Program class looks like 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 <Startup> ());
      }
}

The Program class in ASP.NET Core 6

With the introduction of the simplified hosting model in ASP.NET Core 6, you no longer have to use the Startup class. You can read more about this in my earlier article here. Here’s how you would write a typical 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 in ASP.NET Core 5

The following code snippet shows how you can add a middleware component in ASP.NET Core 5. In our example, we’ll add the response compression middleware.

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

Add middleware in ASP.NET Core 6

To add a middleware component in ASP.NET Core 6, you can use the following code.

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

Add routing in ASP.NET Core 5

To add an endpoint in ASP.NET Core 5, you can use the following code.

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

Add routing in ASP.NET Core 6

You can add an endpoint in ASP.NET Core 6 using the following code.

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

Note that in ASP.NET Core 6 you can add endpoints to WebApplication without having to make explicit calls to the UseRouting or UseEndpoints extension methods.

Add services in ASP.NET Core 5

The following code snippet illustrates how you can add services to the container in 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<IProductRepository, ProductRepository>();
    }
}

Add services in ASP.NET Core 6

To add services to the container in ASP.NET Core 6, you can use the following code.

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<IProductRepository, ProductRepository>();
var app = builder.Build();

Test an ASP.NET Core 5 or ASP.NET Core 6 application

You can test an ASP.NET Core 5 application using either TestServer or WebApplicationFactory. To test using TestServer in ASP.NET Core 5, you can use the following code snippet.

[Fact]
public async Task GetProductsTest()
{
    using var host = Host.CreateDefaultBuilder()
        .ConfigureWebHostDefaults(builder =>
        {
            builder.UseTestServer()
                    .UseStartup<WebApplication1.Startup>();
        })
        .ConfigureServices(services =>
        {
            services.AddSingleton<IProductService, MockProductService>();
        })
        .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<Program>()
        .WithWebHostBuilder(builder =>
        {
            builder.ConfigureServices(services =>
            {
                services.AddSingleton<IProductService, MockProductService>();
            });
        });
    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 in ASP.NET Core 5

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

You can use the ClearProviders method to clear all logging providers and add a specific logging provider or your own custom logging provider. The following code snippet illustrates how you can remove all ILoggerProvider instances and add the Console logging provider in ASP.NET Core 5.

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

Add a logging provider in ASP.NET Core 6

In ASP.NET Core 6, when you call WebApplication.CreateBuilder, it adds the Console, Debug, EventLog, and EventSource logging providers. The following code snippet shows how you can clear the default logging providers and add only the Console logging provider in ASP.NET Core 6.

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();

The code examples provided here illustrate the different ways we add middleware, routing, services, and logging providers in ASP.NET Core 5 and in ASP.NET Core 6, as well as differences in the Program class and testing. These snippets should help you when working with ASP.NET Core 6 applications, and get you off to a good start when you migrate your ASP.NET Core 5 applications to ASP.NET Core 6.

Copyright © 2022 IDG Communications, Inc.



Source link

ShareTweetSend
Previous Post

How to watch the Tory leadership contest like a pro – POLITICO

Next Post

Decentralized Casinos Are The Future Of Online Gambling

Related News

Transform Your Laptop Setup with These 5 Affordable Accessories
Tech

Transform Your Laptop Setup with These 5 Affordable Accessories

November 15, 2026
Microsoft 365 Services Gradually Restore Functionality Following Global Outage
Tech

Microsoft 365 Services Gradually Restore Functionality Following Global Outage

November 14, 2026
Epic Seeks Accountability for Apple’s Alleged Disregard of Court Ruling Intent
Tech

Epic Seeks Accountability for Apple’s Alleged Disregard of Court Ruling Intent

November 13, 2026
European Cybersecurity Firms Make Strides Against U.S. Competitors, Yet American Companies Still Command 62% of the Market
Tech

European Cybersecurity Firms Make Strides Against U.S. Competitors, Yet American Companies Still Command 62% of the Market

November 13, 2026
Next Post
Decentralized Casinos Are The Future Of Online Gambling

Decentralized Casinos Are The Future Of Online Gambling

Discussion about this post

Subscribe To Our Newsletters

    Customer Support


    1251 Wilcrest Drive
    Houston, Texas
    77042 USA
    Call-832.795.1420
    e-mail – news@theinsightpost.com

    Subscribe To Our Newsletters

      Categories

      • Africa
      • Africa-East
      • African Sports
      • American Sports
      • Arts
      • Asia
      • Australia
      • Business
      • Business Asia
      • Business- Africa
      • Canada
      • Defense
      • Education
      • Egypt
      • Energy
      • Entertainment
      • Europe
      • European Soccer
      • Finance
      • Germany
      • Ghana
      • Health
      • Insight
      • International
      • Investing
      • Japan
      • Latest Headlines
      • Life & Living
      • Markets
      • Mobile
      • Movies
      • New Zealand
      • Nigeria
      • Politics
      • Scholarships
      • Science
      • South Africa
      • South America
      • Sports
      • Tech
      • Travel
      • Travel-Africa
      • UK
      • USA
      • Weather
      • World
      No Result
      View All Result

      Recent News

      Gators Shock Wildcats 21-14 in Thrilling Upset Amid Rollercoaster Season

      Gators Shock Wildcats 21-14 in Thrilling Upset Amid Rollercoaster Season

      November 15, 2026
      Government Announces Historic  Billion Investment in Energy Infrastructure Development

      Government Announces Historic $2 Billion Investment in Energy Infrastructure Development

      November 15, 2026
      Notre Dame Postgame Insights: Dive into Superchats with Westwood One Sports

      Notre Dame Postgame Insights: Dive into Superchats with Westwood One Sports

      November 15, 2026
      Transform Your Laptop Setup with These 5 Affordable Accessories

      Transform Your Laptop Setup with These 5 Affordable Accessories

      November 15, 2026
      • Home
      • Advertise With Us
      • About Us
      • Corporate
      • Consumer Rewards
      • Forum
      • Privacy Policy
      • Social Trends

      Theinsightpost ©2026 | All Rights Reserved. Theinsightpost is an Elnegy LLC company, registered in Texas, USA

      Welcome Back!

      Login to your account below

      Forgotten Password?

      Retrieve your password

      Please enter your username or email address to reset your password.

      Log In

      Add New Playlist

      We are using cookies to give you the best experience on our website.

      You can find out more about which cookies we are using or switch them off in .

      No Result
      View All Result
      • Home
      • Insight
      • Blog
      • Business
      • Entertainment
      • Health
      • Politics
      • Shop
        • Gift Shop
        • Value Shop
        • Store
        • Bargain Shop
        • Discount
      • Sports
      • Tech
      • Travel
      • USA
      • Video
      • World
        • Asia
        • Africa
        • South America
        • North America
        • Europe
        • Oceania

      Theinsightpost ©2026 | All Rights Reserved. Theinsightpost is an Elnegy LLC company, registered in Texas, USA

      The Insight Post
      Powered by  GDPR Cookie Compliance
      Privacy Overview

      This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

      Strictly Necessary Cookies

      Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

      Cookie Policy

      More information about our Cookie Policy