asp.net mvc 到 asp.net core3.1 mvc 迁移

本文档记录了将ASP.NET WebAPI网站迁移到ASP.NET Core 3.1过程中遇到的问题及解决方案,包括HttpContent.Current的获取、gRPC调用、JSON序列化、HTTP状态码处理、URL参数解析和Basic Authentication的配置。通过这些步骤,可以成功解决迁移中的不平滑问题。
摘要由CSDN通过智能技术生成

目标 

把以前的asp.net web api网站迁移到asp.net core3.1,过程很不平滑,磕磕绊绊。

1.HttpContent.Current 找不到了

core里面需要先在 Startup.cs 的 ConfigureServices 方法里加一句services.AddHttpContextAccessor(),再通过 HttpContextAccessor.HttpContext 获得这个HttpContent.Current,参考这篇文章

2.调用不了 gRPC http 服务

gRPC 服务只提供了 http 地址,没有提供 https 地址,调用不到,根据微软官方文档,需要在Cofigure方法中加上:

// This switch must be set before creating the GrpcChannel/HttpClient.
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

3.Web Api返回对象属性默认 camel 小写了

要保持对象属性自身的大小写不变,而不是默认首字母小写,需要在ConfigureServices方法中加上:

services.AddControllers().AddJsonOptions(options =>{options.JsonSerializerOptions.PropertyNamingPolicy = null;});

4.Action返回 null 时不再是200而是204 

如果仍然要返回200+null,那需要在ConfigureServices方法中加上:

services.AddControllers().AddJsonOptions(options =>{options.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>(); });

5.URL地址中的 null 不能给 int? 参数赋值

有一个Controller C的HttpGet 方法参数是 (int? a, int? b),url类似 ***/Api/C?a=null&b=1,在asp.net mvc可以,在core里不行, url里面的null 会被解析为string,然后报错:'null' is not valid。

解决(将就)的办法是,把 int? 改成 string 类型,然后判断其值是否为"null"。

6.BasicAuthentication

BasicAuthentication 通过这篇文章实现了。

最后,Startup代码

代码中 AllowEmptyInputInBodyModelBinding 似乎没啥用;

WebHelper只是我定义的静态类,用来挂一个 HttpContextAccessor 方便别的地方用;

DateTimeConverter 用于改变DateTime的Json序列化格式,默认格式有3位毫秒值。

    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.AddCors(options =>
            {
                options.AddPolicy("Open", builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
            });

            services.AddControllers(x =>
            {
                x.AllowEmptyInputInBodyModelBinding = true;
                x.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
            }).AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.PropertyNamingPolicy = null;
                options.JsonSerializerOptions.Converters.Add(new DateTimeConverter());
            });//Pascal 大小写.

            services.AddHttpContextAccessor();

            // configure basic authentication 
            services.AddAuthentication("BasicAuthentication")
                .AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicAuthentication", null);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            WebHelper.HttpContextAccessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseCors("Open");

            // This switch must be set before creating the GrpcChannel/HttpClient.
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }

    public class DateTimeConverter : JsonConverter<DateTime>
    {
        public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            return DateTime.Parse(reader.GetString());
        }

        public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss"));
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值