1.ASP.NET Core 2.2 迁移到3.1
1.1ASP.NET Core 2.2 迁移到3.0
官方文档已详细地记录了所有的操作步骤,地址如下:
注意事项
Newtonsoft.json(Json.NET)支持:一定要安装AspNetCore NewtonsoftJson包
更新 Startup.ConfigureServices 以调用 AddNewtonsoftJson
services.AddMvc()
.AddNewtonsoftJson();
AddNewtonsoftJson 与新 MVC 服务注册方法兼容:
- AddRazorPages
- AddControllersWithViews
- AddControllers
services.AddControllers()
.AddNewtonsoftJson();
可以在对 AddNewtonsoftJson的调用中设置 Newtonsoft.Json 设置:
services.AddMvc()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver());
1.2ASP.NET Core 3.0 迁移到3.1
把所有nuget包升级到3.1就可以了,官网地址如下:
2.Swagger 4.0 迁移到5.0
2.1升级nuget包
Install-Package Swashbuckle.AspNetCore.Swagger -Version 5.0.0
2.2Startup文件修改
- Info类改为OpenApiInfo
- Contact类改为OpenApiContact
- License类改为OpenApiLicense
可能有遗漏,基本就是X类改为OpenApiX类
2.3Swagger 添加 token Header修改
- 方法签名修改
- 部分类修改,与Startup文件中修改类似,基本上也是X类改为OpenApiX类
原来的实现:
public void Apply(Operation operation, OperationFilterContext context)
{
if (operation.Parameters == null) operation.Parameters = new List<IParameter>();
var attrs = context.ApiDescription.ActionDescriptor.AttributeRouteInfo;
//先判断是否是匿名访问,
var descriptor = context.ApiDescription.ActionDescriptor as ControllerActionDescriptor;
if (descriptor != null)
{
var actionAttributes = descriptor.MethodInfo.GetCustomAttributes(inherit: true);
bool isAnonymous = actionAttributes.Any(a => a is AllowAnonymousAttribute);
//非匿名的方法,链接中添加accesstoken值
if (!isAnonymous)
{
operation.Parameters.Add(new NonBodyParameter()
{
Name = CommonConstant.AuthenticationHeader,
In = "header",//query header body path formData
Type = "string",
Required = false, //是否必选
Description= "此处为描述"
});
}
}
}
现在的实现:
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (operation.Parameters == null) operation.Parameters = new List<OpenApiParameter>();
var attrs = context.ApiDescription.ActionDescriptor.AttributeRouteInfo;
//先判断是否是匿名访问,
var descriptor = context.ApiDescription.ActionDescriptor as ControllerActionDescriptor;
if (descriptor != null)
{
var actionAttributes = descriptor.MethodInfo.GetCustomAttributes(inherit: true);
bool isAnonymous = actionAttributes.Any(a => a is AllowAnonymousAttribute);
//非匿名的方法,链接中添加accesstoken值
if (!isAnonymous)
{
operation.Parameters.Add(new OpenApiParameter()
{
Name = CommonConstant.AuthenticationHeader,
In = ParameterLocation.Header,//query header body path formData
Schema = new OpenApiSchema()
{
Type = "string",
},
Required = false, //是否必选
Description = "此处为描述"
});
}
}
}