Example Analysis of ASP.NET Core 3.0 Migration
This article is to share with you the content of a sample analysis of ASP.NET Core 3.0 migration. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
one。 Preface
.net Core 3.0 will be officially released at the .NET Conf conference. As of today, 9 preview versions have been released, with a lot of changes. Due to the lack of continuous attention, it took a lot of time to migrate the previous open source dynamic WebApi project to .NET Core 3.0. I would like to share with you the holes I encountered during the migration process. The migrated version is the latest version of Release. Net Core 2.2 to .NET Core 3.0 Preview 9.
II. ASP.NET Core project migration
Official migration documentation: migration from ASP.NET Core 2.2 to 3.0, this official document is more detailed, but there are some things that are not written in it.
1. Change the frame version
Change the TargetFramework version to netcoreapp3.0
two。 Remove Nuget package
Remove all Nuget packages
Update the remaining Nuget packages to support .NET Core 3.0
3.Program change
Public class Program {public static void Main (string [] args) {CreateHostBuilder (args). Build (). Run ();} public static IHostBuilder CreateHostBuilder (string [] args) = > Host.CreateDefaultBuilder (args) .ConfigureWebHostDefaults (webBuilder = > {webBuilder.UseStartup ();});}
4.Startup change
ConfigureServices method:
Services.AddMvc () .SetCompatibilityVersion (CompatibilityVersion.Version_2_2); change to services.AddControllers () (WebApi) / services.AddControllersWithViews (); (MVC)
Configure method:
1. The API type for obtaining Host environment information in this method, IHostingEnvironment is changed to IWebHostEnvironment.
2. App.UseMVc is changed to:
WebApi:
App.UseRouting (); app.UseAuthorization (); app.UseEndpoints (endpoints = > {endpoints.MapControllers ();})
MVC:
App.UseRouting (); app.UseAuthorization (); app.UseEndpoints (endpoints = > {endpoints.MapControllerRoute (name: "default", pattern: "{controller=Home} / {action=Index} / {id?}");})
About Json components
ASP.NET Core 3.0 removes Newtonsoft.Json by default and uses System.Text.Json implemented by Microsoft. If you want to change it to Newtonsoft.Json, you can take the following two steps:
1. Install the Nuget package:
Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson
two。 Register
Services.AddControllers () .AddNewtonsoftJson ()
three。 Class Library (Class Library Net Standard 2.0) Project Migration
Because of the changes to the meta-package mechanism in ASP.NET Core 3.0, Microsoft.AspNetCore.All or Microsoft.AspNetCore.App 3.0 versions cannot be installed through nuget, and most of the Nuget packages they contain cannot be installed through nuget (there is no corresponding version 3.0). If you also refer to the 2.2 version of the nuget package, there may be errors in running it. The meta-package is included in the .NET Core SDK, which means that if our class library project relies on AspNetCore-related components, we will no longer be able to set the project target framework to .NET Standard, only to .NET Core 3.0, because ASP.NET Core 3.0 only run on .NET Core.
Reason for meta-package mechanism change: https://github.com/aspnet/AspNetCore/issues/3608
1. Change the frame version
two。 Update the Nuget package
Remove Microsoft.AspNetCore.* does not have a version of .NET Core 3.0, for example:
Add a FrameworkReference (not PackageReference) reference:
Thank you for reading! This is the end of this article on "sample Analysis of ASP.NET Core 3.0 Migration". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!