How to update .NET core 5.0 to 6.0

Kevin (Xiaocong) Zheng
2 min readMar 18, 2022

There is a Microsoft’s official documents talking about how to migrate from ASP.NET core 5.0 to 6.0, we could follow this document to do the updates.

Migrate from ASP.NET Core 5.0 to 6.0 .

We could also follow our own case. In our case, we could create a blank ASP.NET core 6.0 project and then move our own codes to that project.

To achieve that goal, first we need to update our Visual Studio to 2022, the simplest way is to uninstall the previous version and then install the Visual Studio 2022(please backup your codes to gitlab before you do that).

Then we could create a new ASP.NET core 6.0 project(rename it to your own project’s name). The biggest change of .NET 6.0 compare to .NET 5.0 is it unifies Startup.cs and Program.cs into a single Program.cs file. So we need to move some contents from Startup.cs to Program.cs, We could take Microsoft’s documents as an example.

I will and some points it doesn’t mention. First, we could use those two lines of code to get configuration and environment settings.

ConfigurationManager configuration = builder.Configuration;
IWebHostEnvironment environment = builder.Environment;

And these lines are used for add a config option.

builder.Services.AddOptions();
builder.Services.Configure<Settings>(configuration.GetSection(“SettingKeys”));

And this one is for how to add a DBcontext.

builder.Services.AddDbContext<dbStudentPortal_stagingContext>(options => options.UseSqlServer(configuration.GetConnectionString(“DefaultConnection”)));

This is how we add a scope.

builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IAcademicAdvising, AcademicAdvisingService>();

How to add AddAuthorization.

builder.Services.AddAuthorization(options =>
{
options.AddPolicy(“NotYear1”, policy => policy.RequireClaim(“YearOfStudy”, “17”, “18”, “19”));
options.AddPolicy(“MSPRAccess”, policy => policy.RequireClaim(“MSPRAccess”, “True”));
options.AddPolicy(“Cohort1”, policy => policy.RequireClaim(“MSFCohort”, “Cohort 1”));
options.AddPolicy(“Cohort2”, policy => policy.RequireClaim(“MSFCohort”, “Cohort 2”));
});

How to add Endpoint

app.UseEndpoints(endpoints =>
{
app.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage(“/_Host”);
});

Other part we could refer to the Microsoft’s documents

--

--