.net core2.2升級.net core3.1/.net5
1.目標框架設定為.net core3.1/.net5
2.將所有的nuget包升級為最新版本
3.需要改動的一些地方
Program.cs
public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); }
更改為:
using Microsoft.Extensions.Hosting; 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.cs
ConfigureServices方法
(1)設定api json返回格式
//.netCore2.2 services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddJsonOptions( opt=> { opt.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm"; }); //.net Core3.1 services.AddControllersWithViews(option => { option.EnableEndpointRouting = false; }).SetCompatibilityVersion(CompatibilityVersion.Latest).AddControllersAsServices().AddNewtonsoftJson ( json => { //統一設定JsonResult json.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; } );
(2)路由變化Configure方法
//.net core2.2 app.UseMvc(routes => { routes.MapRoute( name: "default_area", template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); app.UseMvc(routes => { routes.MapRoute( name: "areas", template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); }); //.net 5 app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default_area", pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "areas", pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); });
(3)一些問題
安裝包:Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
ConfigureServices方法
services.AddControllersWithViews().AddRazorRuntimeCompilation();
(4)檢測到包降級: System.Runtime.Handles 從 4.3.0 降級到 4.0.1。直接從專案引用包以選擇不同版本。 MyBlog.Swagger -> Swashbuckle.AspNetCore.Filters 6.0.1 -> Scrutor 3.0.1 -> Microsoft.Extensions.DependencyModel 2.1.0 -> Microsoft.DotNet.PlatformAbstractions 2.1.0 -> System.IO.FileSystem 4.0.1 -> runtime.unix.System.IO.FileSystem 4.3.0 -> System.Runtime.Handles (>= 4.3.0) MyBlog.Swagger -> Swashbuckle.AspNetCore.Filters 6.0.1 -> Scrutor 3.0.1 -> Microsoft.Extensions.DependencyModel 2.1.0 -> Microsoft.DotNet.PlatformAbstractions 2.1.0 -> System.IO.FileSystem 4.0.1 -> System.Runtime.Handles (>= 4.0.1)
在對應專案下安裝System.Runtime.Handles 4.3.0版本的包就好了