ASP.NET Core 8 Web API中,JSON值无法转换为System.DateOnly。
创始人
2024-09-15 03:00:26
0

在ASP.NET Core 8 Web API中,可以使用自定义转换器来解决将JSON值转换为System.DateOnly的问题。下面是一个示例代码:

首先,创建一个自定义转换器类,继承自JsonConverter类,并重写ReadJson和WriteJson方法:

using System;
using System.Buffers;
using System.Text.Json;
using System.Text.Json.Serialization;

public class DateOnlyConverter : JsonConverter
{
    public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        string dateStr = reader.GetString();
        if (DateTime.TryParse(dateStr, out DateTime date))
        {
            return new DateOnly(date);
        }
        else
        {
            throw new JsonException("Invalid date format.");
        }
    }

    public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
    {
        writer.WriteStringValue(value.ToString());
    }
}

然后,在Startup.cs文件的ConfigureServices方法中注册自定义转换器:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Text.Json.Serialization;

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers()
            .AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.Converters.Add(new DateOnlyConverter());
                options.JsonSerializerOptions.IgnoreNullValues = true;
                options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
                options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
            });
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();

        app.UseAuthorization();

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

现在,您可以在您的Web API控制器中使用System.DateOnly类型的参数,而不会出现转换错误:

using Microsoft.AspNetCore.Mvc;
using System;

[ApiController]
[Route("api/[controller]")]
public class DateController : ControllerBase
{
    [HttpPost]
    public IActionResult Post([FromBody] DateOnly date)
    {
        // 处理DateOnly类型的日期
        return Ok(date);
    }
}

这样,当您将带有日期值的JSON请求发送到该控制器的Post动作时,ASP.NET Core 8 Web API将能够将JSON值正确转换为System.DateOnly类型。

相关内容

热门资讯

2分钟辅助!创思维激k有挂吗,... 【亲,创思维激k有挂吗 这款游戏可以开挂的,确实是有挂的,很多玩家在这款创思维激k有挂吗中打牌都会发...
第十分钟辅助!wepoker亲... 第十分钟辅助!wepoker亲友圈有用吗,丹东约战麻将辅助器(一分钟了解开挂辅助脚本);亲真的是有正...
6分钟开挂!wepoker透视... 6分钟开挂!wepoker透视脚本下载,闲逸辅助脚本(透视透视开挂辅助神器);无需打开直接搜索微信(...
5分钟辅助!皮皮胡子辅助,潮友... 5分钟辅助!皮皮胡子辅助,潮友会虾蟹骰子概率计算方式(玩家必看开挂辅助安装);潮友会虾蟹骰子概率计算...
1分钟辅助!微信微乐有挂没有,... 1分钟辅助!微信微乐有挂没有,微信中至有辅助吗(透视有挂开挂辅助平台);相信小伙伴都知道这个微信中至...
第1分钟辅助!禅游游戏辅助,乐... 第1分钟辅助!禅游游戏辅助,乐平包王攻略(科普常识开挂辅助平台);大家肯定在之前乐平包王攻略或者乐平...
第9分钟开挂!广西微乐小程序脚... 第9分钟开挂!广西微乐小程序脚本,河洛杠次插件(最新技巧开挂辅助下载)【无需打开直接搜索加薇1367...
第4分钟辅助!贪玩互娱辅助,微... 第4分钟辅助!贪玩互娱辅助,微乐小程序挂哪里有(今日重大通报开挂辅助工具);相信小伙伴都知道这个微乐...
第七分钟开挂!欢聚水鱼天下辅助... 您好:这款欢聚水鱼天下辅助游戏是可以开挂的,确实是有挂的,很多玩家在这款欢聚水鱼天下辅助游戏中打牌都...
第四分钟辅助!xpoker透视... 第四分钟辅助!xpoker透视辅助,小闲川南手游辅助器(技巧知识分享开挂辅助安装);小薇(透视辅助)...