Migrate from ASP.NET Core 2.0 to 3.0 Migrate from ASP.NET Core 2.0 to 3.0 asp.net asp.net

Migrate from ASP.NET Core 2.0 to 3.0


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

https://docs.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.


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 withCreateWebHostBuilder.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 noviews, Add services.AddControllers()

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

  • If you want to develop a Model View Controller (i.e. MVCapplication), 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.JsonBut 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 mustappear between the calls to UseRouting and UseEndpoints to beeffective

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