随着技术的不断拓展更新,我们所使用的技术也在不断地升级优化,项目的框架也在不断地升级,本次讲解 .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. P类问题,NP,NPC,HPHard,coNP,NPI问题 的简单认识

    参考<算法设计技巧与分析>--沙特 问题可以分为判定类问题和最优化问题,判定类问题可以转化为最优化问题,所以下面讨论的是判定类的问题. P类问题是可以在多项式时间  采用确定性算法给出解 ...

  2. 2017CCCC决赛 L1-3. 阅览室

    L1-3 阅览室(20 分) 天梯图书阅览室请你编写一个简单的图书借阅统计程序.当读者借书时,管理员输入书号并按下S键,程序开始计时:当读者还书时,管理员输入书号并按下E键,程序结束计时.书号为不超过 ...

  3. 地址解析协议ARP与逆地址解析协议RARP

    IP地址是用来通信的,但是和硬件地址是有区别的.物理地址是数据链路层和物理层使用的地址,IP地址是网络层及以上各层使用的地址. 发送数据时,数据从高层向下层传输,使用IP地址的IP数据报交给下层的数据 ...

  4. value-key

    value-key object 如果 Select 的绑定值为对象类型,请务必指定 value-key 作为它的唯一性标识. value-key 作为 value 唯一标识的键名,绑定值为对象类型时 ...

  5. 网站备案查询/ICP备案查询网

    网站备案查询/ICP备案查询网 互联网站备案信息全国公安机关互联网站安全服务平台http://www.beian.gov.cn/portal/index 1 http://www.miitbeian. ...

  6. js Array.from & Array.of All In One

    js Array.from & Array.of All In One 数组生成器 Array.from The Array.from() static method creates a ne ...

  7. 微信小程序 HTTP API

    微信小程序 HTTP API promise API https://www.npmtrends.com/node-fetch-vs-got-vs-axios-vs-superagent node-f ...

  8. css text-align-last & text-align

    css text-align-last & text-align css https://caniuse.com/mdn-css_properties_text-align-last http ...

  9. switchable css dark theme in js & html custom element

    switchable css dark theme in js & html custom element dark theme / dark mode https://codepen.io/ ...

  10. Flutter 1.17.x

    Flutter 1.17.x Flutter (Channel stable, v1.17.3, on Mac OS X 10.15.5 19F101, locale en-CN) https://f ...