.NET Core 2.X upgrade 3.1

Simple personal record 3.1 upgrade process.

 

1. Modify the goal frame

 

 

 Search Solutions --netcoreapp- behind all changed a few points 3.1

 

In search of the file, to see whether there are two Microsoft.AspNetCore ... ----- remove the PackageReference, as it has been included in the framework of the objectives of the 3.1.

<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />

  

 

 

2. upgrade package depends NuGet

The project depends on all packages to the latest version NuGet

 

 

 

 

 

3. Start Change

Program.cs

Future versions will be deprecated WebHostBuilder, and replace it  HostBuilder.

 

The original unmodified code before

 

 

 

 

HostBuilder replace WebHostBuilder

Modified

 

 

 

 

 Startup.cs

The following figure shows the application has been removed and changed the line:

 

 

 

 On the map, marked red is the code you want to delete.

 

 

 

 

 In the figure, the added code is displayed in green.

 public void ConfigureServices(IServiceCollection services)
{
        services.AddControllersWithViews();   //控制器+视图=mvc模式
        services.AddControllers();   //单控制器=WebApi
        services.AddRazorPages();   //Razor Pages
}

 

路由启动方式

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
    name: "area",
    pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

    endpoints.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
});

 

 

改到这差不多就改完了。然后删除掉项目中的隐藏文件   .vs     .vscore       .idea     然后重启项目,重新生成下解决方案即可。

----------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

如果刚好你也用  Nlog 日志 

原写法

loggerFactory.AddNLog();//添加NLog
env.ConfigureNLog("LogConfig/nlog.config");//读取Nlog配置文件 

修改为

loggerFactory.AddNLog();//添加NLog
NLog.Web.NLogBuilder.ConfigureNLog("LogConfig/nlog.config");//读取Nlog配置文件

 

 

又刚好你们返回json是差不多和我一样返回的

 

 需要在 Startup.cs 添加 Newtonsoft.json

 public void ConfigureServices(IServiceCollection services)
{           
     services.AddMvc().AddNewtonsoftJson(options =>
     {
                //忽略循环引用
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                //不使用驼峰样式的key
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
      });
 }

ASP.NET Core 的默认 JSON 序列化程序现已 System.Text.Json,这是 .NET Core 3.0 中的新增项。

所以可能会有问题,如没问题请忽略。

 

 

从 ASP.NET Core 2.2 迁移到3.0

Guess you like

Origin www.cnblogs.com/ya-jun/p/12340488.html