随着技术的不断拓展更新,我们所使用的技术也在不断地升级优化,项目的框架也在不断地升级,本次讲解 .net core 2.1  升级到3.1所需要注意的事项;

当项目框架升级后,所有的Nuget引用也会对应变化,这些根据自己的框架所使用的技术对应做升级即可,这里不做过多赘述;

其次就是要修改 Program.cs 文件,这里把修改前的和修改后的统一贴出代码做调整,代码如下:

2.1版本的文件代码

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using System; namespace S2_Xxxx_XxxNetApi
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
Console.WriteLine("接口启动成功");
// QuartzHelper.ExecuteInterval<Test>(200);
} public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
//.UseUrls("http://*:5000")//发布时需要注释
.UseStartup<Startup>();
}
}

3.1版本的文件代码

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using System; namespace S2_Cggc_PmsNetApi
{
/// <summary>
/// 启动
/// </summary>
public class Program
{
/// <summary>
/// 启动
/// </summary>
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
Console.WriteLine("接口启动成功");
} /// <summary>
/// 启动
/// </summary>
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}

然后就是Startup文件,代码如下:

2.1版本文件代码:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Quartz;
using Quartz.Impl;
using S2_Xxxx_XxxNetApi;
using System.Linq; namespace S2_Xxxx_XxxNetApi
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
}); //调度任务注册
services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();//注册ISchedulerFactory的实例。 string conn = Configuration.GetSection("AppSettings:ConnectString").Value;
MySqlHelper.Conn = conn; string orclconn = Configuration.GetSection("AppSettings:OrclConnectString").Value;
OracleHelper.connectionString = orclconn; string redisconn = Configuration.GetSection("AppSettings:RedisConnectString").Value;
RedisHelper.SetCon(redisconn);
RedisHelper.BuildCache(); services.Configure<AppSettings>(Configuration.GetSection("AppSettings")); //跨域支持
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
corsBuild => corsBuild.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
}); services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDistributedMemoryCache();//启用session之前必须先添加内存
//services.AddSession();
services.AddSession(options =>
{
options.Cookie.Name = ".AdventureWorks.Session";
options.IdleTimeout = System.TimeSpan.FromSeconds(1200);//设置session的过期时间
options.Cookie.HttpOnly = true;//设置在浏览器不能通过js获得该cookie的值
}); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>(); services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
{
Version = "v1",
Title = "接口文档"
});
options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
});
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
//app.usehets();
}
app.UseSession();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy(); app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebAPI文档");
}); //跨域支持
app.UseCors("CorsPolicy");
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
}); }
}
}

3.1版本文件代码:

using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Quartz;
using Quartz.Impl;
using System; namespace S2_Cggc_PmsNetApi
{
/// <summary>
/// 启动
/// </summary>
public class Startup
{
/// <summary>
/// 启动
/// </summary>
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} /// <summary>
/// 启动
/// </summary>
public IConfiguration Configuration { get; } /// <summary>
/// 启动
/// </summary>
public void ConfigureServices(IServiceCollection services)
{ //启用session之前必须先添加内存
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.Cookie.Name = ".AdventureWorks.Session";
options.IdleTimeout = System.TimeSpan.FromSeconds(1200);//设置session的过期时间
options.Cookie.HttpOnly = true;//设置在浏览器不能通过js获得该cookie的值
}); services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => false;//这里要改为false,默认是true,true的时候session无效
options.MinimumSameSitePolicy = SameSiteMode.None;
}); services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(); services.AddCors(options =>
{
options.AddPolicy("any", builder => { builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); });
}); //注册过滤器
services.AddSingleton<ApiResultFilterAttribute>();
services.AddSingleton<ApiExceptionFilterAttribute>(); services.AddMvc(
config =>
{
config.EnableEndpointRouting = false;
config.Filters.AddService(typeof(ApiResultFilterAttribute));
config.Filters.AddService(typeof(ApiExceptionFilterAttribute));
})
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddNewtonsoftJson(); //services.AddMvc(options => { options.Filters.Add<ResultFilterAttribute>(); }); services.AddSwaggerDocument(); //注册Swagger 服务
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
//调度任务注册
services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();//注册ISchedulerFactory的实例。
QuartzHelper.AddJobForSeconds<Deduction>();
QuartzHelper.Start();
} /// <summary>
/// 启动
/// </summary>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSession();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization();
app.UseCors("any");
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
app.UseOpenApi(); //添加swagger生成api文档(默认路由文档 /swagger/v1/swagger.json)
app.UseSwaggerUi3();//添加Swagger UI到请求管道中(默认路由: /swagger).
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes => { routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}"); });
}
}
}

做了对应调整后,编译后检查是否还有错误提示,即可升级成功,可能有些控制器里面的属性写法也会有些变化,这个还没有具体深入研究,欢迎小伙伴们做补充,我将虚心接受大家的意见,感谢~

.Net Core 2.1 升级3.1 问题整理的更多相关文章

  1. 将 ASP.NET Core 2.1 升级到最新的长期支持版本ASP.NET Core 3.1

    目录 前言 Microsoft.AspNetCore.Mvc.ViewFeatures.Internal 消失了 升级到 ASP.NET Core 3.1 项目文件(.csproj) Program. ...

  2. Entity Framework Core 1.1 升级通告

    原文地址:https://blogs.msdn.microsoft.com/dotnet/2016/11/16/announcing-entity-framework-core-1-1/ 翻译:杨晓东 ...

  3. asp.net core 1.1 升级后,操作mysql出错的解决办法。

    遇到问题 core的版本从1.0升级到1.1,操作mysql数据库,查询数据时遇到MissingMethodException问题,更新.插入操作没有问题. 如果你也遇到这个问题,请参照以下步骤进行升 ...

  4. ASP.NET Core 2.0升级到3.0的变化和问题

    前言 在.NET Core 2.0发布的时候,博主也趁热使用ASP.NET Core 2.0写了一个独立的博客网站,现如今恰逢.NET Core 3.0发布之际,于是将该网站进行了升级. 下面就记录升 ...

  5. .Net Core 2.2升级3.1的避坑指南

    写在前面 微软在更新.Net Core版本的时候,动作往往很大,使得每次更新版本的时候都得小心翼翼,坑实在是太多.往往是悄咪咪的移除了某项功能或者组件,或者不在支持XX方法,这就很花时间去找回需要的东 ...

  6. .Net Core 1.0升级2.0(xproj项目迁移到.csproj )

    vs2015的创建的项目是以*.xproj的项目文件,迁移到vs2017需要如下准备: 1.安装好vs2017(废话) 2.下载最新的SDK和 .NET Core 2.0 Preview 1 Runt ...

  7. centos下 .net core 2.0 升级 到 2.1 遇到的一个小问题

    .net core 2.0的安装方式,可能不是用yum方式安装的,所以,在用yum安装2.1之后,无法运行.net core 所以用来下面的这个命令,重新映射一下dotnet目录. ln -s /us ...

  8. [.NET Core 32]升级vs code之后,vs code无法调试net core web项目

    错误提示&处理方法 参考链接:https://github.com/OmniSharp/omnisharp-vscode/issues/1742 错误:The .NET Core debugg ...

  9. Net core 2.x 升级 3.0 使用自带 System.Text.Json 时区 踩坑经历

    .Net Core 3.0 更新的东西很多,这里就不多做解释了,官方和博园大佬写得很详细 关于 Net Core 时区问题,在 2.1 版本的时候,因为用的是 Newtonsoft.Json,配置比较 ...

随机推荐

  1. Vmware 15.5 ubuntu 12.04.5-desktop-i386.iso insmod后死机

    就是makefile没有问题,在其他同学的相同环境下也没有问题,但是在我的虚拟机里就会死机,复制了其他同学的虚拟机过来也会死机,所以猜想是VMware的问题. 于是下载了Virtual box,然后安 ...

  2. Leetcode(35)-搜索插入位置

    给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 这个题目很简单,因为它是给定的排序数组而且没有重 ...

  3. u-boot 移植 --->7、u-bootl流程粗线条梳理

    通过前面的调试了解到s5pv210这个芯片的启动流程是需要将u-boot分为两部分的分别为SPL和u-boot.这里我使用网上的方式不直接使用u-boot的SPL连接脚本单独生成SPL的image而是 ...

  4. 推荐一个vuepress模板,一键快速搭建文档站

    介绍 vuepress-template是一个简单的VuePress案例模板,目的是让用户可以直接clone这个仓库,作为初始化一个VuePress网站启动项目,然后在这个项目的基础上新增自定义配置和 ...

  5. vue & async mounted

    vue & async mounted refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

  6. Microsoft Lifecycle Policy

    Microsoft Lifecycle Policy The Microsoft Lifecycle Policy gives you consistent and predictable guide ...

  7. H5 APP 页面移动端适配方案

    H5 APP 页面移动端适配方案 https://segmentfault.com/a/1190000011586301 https://juejin.im/post/5cbdee71f265da03 ...

  8. jquery.query.js

    帮助文档 var url = location.search; > "?action=view&section=info&id=123&debug&te ...

  9. Masterboxan INC是你靠近财富的最佳选择

    Masterboxan INC万事达资产管理有限公司(公司编号:20151264097)是一家国际性资产管理公司,主要提供外汇.证券.投资管理和财富管理等金融服务,其在投资方面一直倡导组合型投资构建稳 ...

  10. 使用stress进行压力测试

    本文转载自使用stress进行压力测试 导语 stress,顾名思义是一款压力测试工具.你可以用它来对系统CPU,内存,以及磁盘IO生成负载. 安装stress 几乎所有主流的linux发行版的软件仓 ...