Hurry Up! Upgrade Your .NET Core to Latest ASP.NET Core 3! – Windows ASP.NET Core Hosting 2024 | Review and Comparison

This article will shows how to upgrade your previous .NET Core to latest ASP.NET Core 3.0.

I used a new ASP.NET Core 3.0 Web Application (Razor Pages) project with Identity and the documentation to migrate the ASP.NET Core 2.2 – Bootstrap Native Project to .NET Core 3.0. When I tried to upgrade my aplication to .NET Core 3.0 I found a couple of issues. You should migrate a copy of your project to analyze and mitigate any issues.

Let’s start by editing the project file. Right click on the project then click Edit Project File.

Edit PROJECTNAME.csproj:
Update the Target Framework:
<TargetFramework>netcoreapp3.0</TargetFramework>
Optionally remove the AspNetCoreHostingModel property if its value is InProcess:
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
If TypeScriptToolsVersion, update version to Latest:
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
Remove obsolete package references:
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />

Now use the Nuget Manager to install new packages. Right click on the project then click Manage NuGet Packages. The Microsoft.AspNetCore.Mvc.NewtonsoftJson package is required for proper Admin > Users > Details serialization. 

On the Browse tab, search and install the following packages:
  • Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
  • Microsoft.AspNetCore.Identity.EntityFrameworkCore
  • Microsoft.AspNetCore.Mvc.NewtonsoftJson
  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools
On the Installed tab, update:
  • Microsoft.VisualStudio.Web.CodeGeneration.Design

Update the Program class to use IHostBuilder.

Edit Program.cs, implement IHostBuilder:
public static void Main(string[] args)
{
    CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) => Host
    .CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder
        .ConfigureKestrel(serverOptions =>
        {
            // Set properties and call methods on options
        })
        .UseStartup<Startup>();
    });

Update the namespaces with using Microsoft.Extensions.Hosting;.

Use Find and Replace > Current Project to replace IHostingEnvironment with IWebHostEnvironment then update the namespaces with using Microsoft.Extensions.Hosting;.

Edit IdentityHostingStartup.cs in the Areas > Identity > Pages folder, use Remove and Sort Usings to remove the namespace using Microsoft.AspNetCore.Identity.UI;.

Update the configuration in the Startup class.

Edit Startup.cs > ConfigureServices:
Remove:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
    .AddRazorPagesOptions(options =>
    {
        options.AllowAreas = true;
        options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
        options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
        options.Conventions.AuthorizeAreaPage("Admin", "/Index");
        options.Conventions.AuthorizeAreaFolder("Admin", "/Users");
    });
Add:
services.AddRazorPages(options =>
    {
        options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
        options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
        options.Conventions.AuthorizeAreaPage("Admin", "/Index");
        options.Conventions.AuthorizeAreaFolder("Admin", "/Users");
    })
    .AddNewtonsoftJson();
Edit Startup.cs > Configure:
Add above app.UseAuthentication:
app.UseRouting();
Add below app.UseAuthentication:
app.UseAuthorization();
Remove:
app.UseMvc();
Add:
app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
});

Build, run and test.