• ASP.NET Core 2.2 迁移到3.1


    1.ASP.NET Core 2.2 迁移到3.1

    1.1ASP.NET Core 2.2 迁移到3.0

    官方文档已详细地记录了所有的操作步骤,地址如下:

    https://docs.microsoft.com/zh-cn/aspnet/core/migration/22-to-30?view=aspnetcore-2.1&tabs=visual-studio

    注意事项

    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就可以了,官网地址如下:

    https://docs.microsoft.com/zh-cn/aspnet/core/migration/30-to-31?view=aspnetcore-2.1&tabs=visual-studio

    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 = "此处为描述"
                });
            }
        }
    }
    
  • 相关阅读:
    Ubuntu 15.04 下apt-get安装JDK
    Ubuntu下apt-get安装Java,Tomcat
    虚拟化技术比较 PV HVM
    Java8 Lambda表达式教程
    SpringMVC实现上传和下载
    web.xml中的url-pattern映射规则
    java文件读写操作大全
    Java创建文件
    JAVA文件中获取路径及WEB应用程序获取路径方法
    session.flush()与session.clear()的区别及使用环境
  • 原文地址:https://www.cnblogs.com/TomGui/p/12386681.html
Copyright © 2020-2023  润新知