How to migrate ASP.NET core 3.1 to ASP.NET 6.0

Views: 10484
Comments: 2
Like/Unlike: 2
Posted On: 28-Jun-2022 23:52 

Share:   fb twitter linkedin
Smith
None
2568 Points
74 Posts


ASP.NET 6.0 is a Long Term Support Version (supported for three years). The previous version, .NET Core 3.1 support will be finalized in December 2022, and support for .NET 5 will be ended May 2022. This article explains how to update an existing ASP.NET Core 3.1 project to ASP.NET Core 6.0. 

Prerequisites

  1. Visual Studio 2022
  2. .Net 6.0 SDK

Following are the steps to migrate ASP.NET core 3.1 to ASP.NET 6.0:

  1. Update the target framework:

    Open .csproj file in edit mode and change TargetFramework to .net6.0
    <Project Sdk="Microsoft.NET.Sdk.Web">
      <PropertyGroup>
    -    <TargetFramework>netcoreapp3.1</TargetFramework>
    +    <TargetFramework>net6.0</TargetFramework>
      </PropertyGroup>
    </Project>
  2. Upgrade package to 6.0 if required like:

    <ItemGroup>
    -    <PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="3.1.6" />
    -    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.6" />
    -    <PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="3.1.6" />
    -    <PackageReference Include="System.Net.Http.Json" Version="3.2.1" />
    +    <PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="6.0.0" />
    +    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0" />
    +    <PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="6.0.0" />
    +    <PackageReference Include="System.Net.Http.Json" Version="6.0.0" />
    </ItemGroup>
  3. Delete bin and obj folders and clear cache: 

    We may need to delete the bin and obj folders. Run dotnet nuget locals --clear all to clear the NuGet package cache.
    dotnet nuget locals --clear all
  4. Update Docker images:

    - docker pull mcr.microsoft.com/dotnet/core/aspnet:3.1
    + docker pull mcr.microsoft.com/dotnet/aspnet:6.0

    Examples

    -FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
    +FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
    -FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
    +FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
  5. Minimal hosting model (OPTIONAL):

     The minimal hosting model unifies Startup.cs and Program.cs into a single Program.cs file. ConfigureServices and Configure are no longer used. Apps migrating from ASP.NET Core 3.1 to 6.0 don't need to use the minimal hosting model, using Startup and the Generic Host used by the ASP.NET Core 3.1 templates is fully supported. To use Startup with the new minimal hosting model, see https://docs.microsoft.com/en-us/aspnet/core/migration/50-to-60-samples?view=aspnetcore-6.0

  6. Upgrade to common package which is build on .NET 6.0

  7. Build and run the project in VS 2022 and verify the bin folder, it should contain only .net6.0 libraries.

References

 

2 Comments

great..


Rashmi
08-Aug-2022 at 04:01

Thanks for sharing useful code.


Mithilesh Tata
01-Dec-2022 at 22:40
 Log In to Chat