.net core2.2升级.net core3.1/.net5

2020-12-22 06:31

阅读:454

标签:stat   ash   run   新版本   sem   tst   ddc   host   mapr   

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();
    }

更改为:

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();
             });
    }

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版本的包就好了

 

.net core2.2升级.net core3.1/.net5

标签:stat   ash   run   新版本   sem   tst   ddc   host   mapr   

原文地址:https://www.cnblogs.com/LiChen19951127/p/13986456.html


评论


亲,登录后才可以留言!