- A+
一.ASP.NET Core 项目迁移
官方迁移文档:从 ASP.NET Core 2.2 迁移到3.0 ,这个官方文档比较详细,但是有一些东西里面并没有写。
1.更改框架版本
netcoreapp3.0
2.移除Nuget包
3.Program更改
- 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<Startup>();
- });
- }
4.Startup更改
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddControllers()
services.AddControllersWithViews();
IHostingEnvironment
改为 IWebHostEnvironment
- app.UseRouting();
- app.UseAuthorization();
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapControllers();
- });
- app.UseRouting();
- app.UseAuthorization();
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapControllerRoute(
- name: "default",
- pattern: "{controller=Home}/{action=Index}/{id?}");
- });
ASP.NET Core 3.0 默认移除了 Newtonsoft.Json
,使用了微软自己实现的 System.Text.Json
,如果要改为 Newtonsoft.Json ,那么有以下两步:
- Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson
- services.AddControllers().AddNewtonsoftJson();
二.类库(Class Library Net Standard 2.0)项目迁移
因为 ASP.NET Core 3.0 对元包机制的改动,现在不能通过nuget安装 Microsoft.AspNetCore.All 或者 Microsoft.AspNetCore.App 3.0版本,以及他们包含的大多数Nuget包也不能通过nuget安装了(没有3.0对应的版本)。如果说还引用2.2版本的nuget包,那么运行起来可能会出错。元包被包含在了 .NET Core SDK中,这意味着如果我们的类库项目依赖了 AspNetCore 相关组件,那么将没法继续将项目目标框架设置为 .NET Standard 了,只能设置为.NET Core 3.0,因为 ASP.NET Core 3.0 only run on .NET Core 。
元包机制改动原因:https://github.com/aspnet/AspNetCore/issues/3608
1.更改框架版本
2.更新Nuget包
三.结束
题外话:ASP.NET Core 直到2.2 是可以同时运行在 .NET Framework 和 .NET Core 中,但是从 ASP.NET Core 3.0 开始,将会只支持 .NET Core。
相关资料:A first look at changes coming in ASP.NET Core 3.0
上面说的改动,微软官方都有解释原因,其实是为了变得更好而改动,弥补以前的缺点,只不过对于用了这么久的Core来说有点折腾,但是还是能接受,为了更好的 .NET Core。