Migrate from ASP.NET Core 2.0 to 3.0
Asked Answered
D

2

12

I have an application in ASP.NET Core 2.0. I want to upgrade it to ASP.NET Core 3.0. How can I do that?

Delarosa answered 20/2, 2019 at 5:51 Comment(0)
J
11

Follow this link. This will give some guidance for your migration.

https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-2.2&tabs=visual-studio

As stated in the comments, the complete path of migration from 2.0 to 3.0 would be:

You will have to go through them step by step.

Jenkins answered 20/2, 2019 at 6:12 Comment(3)
Thank you for the reply. I have already checked this link. But I want to do the migration from 2.0 to 3.0 not from 2.2 from 3.0 So I am not sure that this link will work fine or not for my projectDelarosa
Could you please verify and let me knowDelarosa
I think, you may need to collect information about the different dot version migration... example 2.0 to 2.1 then 2.1 to 2.2 finally 2.2 to 3.0. Then collectively you can apply all these changes.Jenkins
C
5

Official documentations and most answers for this question in stackoverflow too, have steps for upgrading .NET core versions one by one (2.0 -> 2.1 -> 2.2 -> 3.0 ->....). However since .NET Core 3.0 has ended it's life, I will give the instructions for upgrading to .NET Core 3.1(LTS) version directly from .NET Core 2.0.

1. Change Target Framework to 3.1

In project file change the TargetFramework <TargetFramework>netcoreapp2.0</TargetFramework> to <TargetFramework>netcoreapp3.1</TargetFramework>

enter image description here

2) Changes to Main

These are the changes in program.cs

public static void Main(string[] args)
        {
            //BuildWebHost(args).Run(); //Remove this in your code
            CreateWebHostBuilder(args).Build().Run(); //Add this instead
        }

        //Remove this in your code
        public static IWebHost BuildWebHost(string[] args) =>
           WebHost.CreateDefaultBuilder(args)
               .UseStartup<Startup>()
               .Build();

        //Add this instead
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                    .UseStartup<Startup>();

The new Main replaces the call to BuildWebHost with CreateWebHostBuilder.IWebHostBuilder was added to support a new integration test infrastructure.

3) Changes to Startup.cs

3.1) AddMvc() Method

You can see a method named AddMvc(). This method has all features. So you create can any type of application (Web API, MVC, and Razor Pages). It will add extra features even though which are not required to your application which might impact the performance of your application.

   services.AddMvc(); //Remove this

Instead,

  • If you want to create a Web API application where there are no views, Add services.AddControllers()

  • If you want to work with the Razor Page application, Add services.AddRazorPages();

  • If you want to develop a Model View Controller (i.e. MVC application), Add services.AddControllersWithViews();

new methods can also be combined

3.2) Newtonsoft.Json (Json.NET) support

Newtonsoft.Json has been removed from the ASP.NET Core shared framework. now default JSON serializer for ASP.NET Core is System.Text.Json But if your application already use it, first install Microsoft.AspNetCore.Mvc.NewtonsoftJson package and then append .AddNewtonsoftJson(); to newly added MVC service registration method.

services.AddControllers()
    .AddNewtonsoftJson();

3.3) Routing startup code

Replace UseMvc with UseEndpoints

   //REMOVE
    //app.UseMvc(routes =>
    //{
    //    routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
    //});
    
    //ADD
    app.UseRouting(); //If your app calls UseStaticFiles, place UseStaticFiles before UseRouting
    app.UseEndpoints(endpoints =>
    {
       endpoints.MapControllerRoute(
               name: "default",
               pattern: "{controller=Home}/{action=Index}/{id?}");
    });

Note : calls to UseAuthentication, UseAuthorization, and UseCors must appear between the calls to UseRouting and UseEndpoints to be effective

3.4) In Configure method,

replace IHostingEnvironment with IWebHostEnvironment

//public void Configure(IApplicationBuilder app, IHostingEnvironment env)
public void Configure(IApplicationBuilder app, IWebHostEnvironment  env)

Add app.UseHttpsRedirection() and for non-development environments add app.UseHsts();

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseHsts();
}

 app.UseHttpsRedirection();

4 Update .NET Core SDK version in global.json

If you rely upon a global.json file to target a specific .NET Core SDK version update the version property to the 3.1 SDK version installed

{
  "sdk": {
    "version": "3.1.101"
  }
}

5) Remove obsolete package references

A large number of NuGet packages aren't produced from versions after ASP.NET Core v3.0. As a example remove package references for Microsoft.AspNetCore.All (When you comming from v2.0) or Microsoft.AspNetCore.App (When you comming from v2.1)

**

6) Update Microsoft package references

In project file update Microsoft package reference's version attribute to 3.1.0 or later. You can do this easily with Nuget package manager too. enter image description here

Crumb answered 19/6, 2021 at 14:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.