Converting a .NET 5 web app to a .NET 6 one

by Patrick Lee on 30 Apr 2022 in categories tech with tags .NET 5.0 .NET 6.0 Visual Studio 2022

I mentioned in this previous post that I had upgraded this site from .NET 5 to .NET 6.

One thing I hadn't done is remove the usage of Startup.cs file.

.NET 5 websites by default have both a Program.cs and a Startup.cs file.

.NET 6 websites only have a Program.cs file.

I had updated the site to .NET 6 merely by changing the target framework in the project's properties), but I had left the existing Program.cs and Startup.cs files unchanged.

Removing the Startup.cs file

Today I wanted to see if I could remove the Startup.cs file. 

It turned out this was relatively easy for my project:

I first replaced the contents of Program.cs by the contents of the Program.cs of a standard new .NET 6 MVC website that I added temporarily to my Visual Studio 2022 solution. 

I then got a compile error on the very first line of Program.cs:

The name 'WebApplication' does not exist in the current context.

Comparing the contents of my project's  .csproj file with that  of the standard new .NET 6 project showed that the latter uses ImplicitUsings.

Adding  

<ImplicitUsings>enable</ImplicitUsings>

to the PropertyGroup in my project file solved the compile problem.

After that, I just had to add some services (previously in Startup.cs) in Program.cs (NB the syntax in Program.cs is slightly different: builder.Services.Add ..) and I was then able to delete the Startup.cs file.