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类型。

相关内容

热门资讯

有挂详细辅助!小程序能不能开挂... >>您好:小程序能不能开挂确实是有挂的,很多玩家在这款小程序能不能开挂游戏中打牌都会发现很多用户的牌...
有挂规律开挂!乐乐游戏辅助器,... 有挂规律开挂!乐乐游戏辅助器,丰县14张脚本(透视了解开挂辅助平台);无需打开直接搜索加(薇:136...
竟然有挂开挂!天天爱柳州麻将辅... 竟然有挂开挂!天天爱柳州麻将辅助器,奇迹陕西辅助(六分钟了解开挂辅助安装) 【无需打开直接搜索加薇1...
有挂规律开挂!闲逸辅助器,微友... 有挂规律开挂!闲逸辅助器,微友辅助器免费版(新手必备开挂辅助软件) >>您好:软件加薇1367043...
确实有挂辅助!丰城双剑辅助,新... 确实有挂辅助!丰城双剑辅助,新九哥开挂(每日必备开挂辅助下载) >>您好:软件加薇136704302...
有挂工具开挂!杭州都莱大菠萝辅... 有挂工具开挂!杭州都莱大菠萝辅助器,微友助手破解版(透视私人局开挂辅助平台) 了解更多开挂安装加(1...
真的有挂开挂!海贝之城正版辅助... 真的有挂开挂!海贝之城正版辅助,哥哥打大a有辅助吗(2分钟细说开挂辅助平台)哥哥打大a有辅助吗ai黑...
有挂方针开挂!潘潘讲故事辅助器... 有挂方针开挂!潘潘讲故事辅助器,人海大厅辅助插件(玩家必备教程开挂辅助插件);是一款可以让一直输的玩...
有挂教程辅助!威信茶馆修改器,... 有挂教程辅助!威信茶馆修改器,wpk辅助最怕三个东西(六分钟了解开挂辅助神器)威信茶馆修改器ai黑科...
有挂详细辅助!边锋老友辅助器,... 有挂详细辅助!边锋老友辅助器,菠萝辅助器免费版的特点(发现一款开挂辅助平台) >>您好:软件加薇13...