Migrating from ASP.NET Core 5.0 to .NET Core 6.0 Print

  • 2

ASP.NET Core is an open-source, cross-platform framework for building web applications. The release of ASP.NET Core 6.0 comes with many new features and improvements, making it an attractive upgrade option for developers who want to take advantage of the latest advancements in web development. In this article, we will walk through the steps to upgrade an existing ASP.NET Core 5.0 project to ASP.NET Core 6.0.

Step 1: Install the .NET 6.0 SDK To upgrade your project, you first need to ensure that you have the .NET 6.0 SDK installed on your machine. You can download and install the .NET 6.0 SDK from the official Microsoft website.

Step 2: Update the project file The next step is to update the project file of your ASP.NET Core 5.0 project. Open the project file (.csproj) and update the TargetFramework property to net6.0:

<TargetFramework>net6.0</TargetFramework>

Step 3: Update the packages After updating the project file, you need to update the packages used in your project. This can be done by either using the Visual Studio NuGet Package Manager or by manually updating the package versions in the project file. You can update the package versions to their latest available versions by using the following command in the Package Manager Console:

Update-Package

Step 4: Update the Startup class ASP.NET Core 6.0 introduces some changes to the Startup class, which requires updating the code to reflect these changes. The following are some of the changes:

  1. The UseRouting method should be called before the UseEndpoints method.
  2. The UseEndpoints method should be passed an action that configures the endpoints.
  3. The UseHttpsRedirection method should be called before the UseRouting method.

Here's what the updated Startup class should look like:

public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); } }

Step 5: Update the appsettings.json file If your project uses the appsettings.json file to store configuration settings, you will need to update it to reflect the changes in ASP.NET Core 6.0. Specifically, you need to update the logging section to use the new logging framework. Here's an example of what the updated appsettings.json file should look like:

{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" }, "Console": { "FormatterName": "Systemd" } } }

Step 6: Run the project and test After updating the project file, packages, Startup class, and appsettings.json file, you can run the project to ensure that everything is working as expected. You may need to make additional changes to your code to account for any breaking changes that may have been introduced in ASP.NET Core 6.0.

Conclusion In this article, we have walked through the steps required to upgrade an existing ASP.NET Core 5.0 project to ASP.NET Core 6.0.

 

For even more info, please view this great article on Microsoft:

https://docs.microsoft.com/en-us/aspnet/core/migration/50-to-60?view=aspnetcore-6.0&tabs=visual-studio


Was this answer helpful?

« Back