码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2020-11-20 12:23:49      阅读:49      评论:0      收藏:0      [点我收藏+]

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

 

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!