System.NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported. Path: $.Columns.DataType.

Jim Olewis 20 Reputation points
2023-01-25T03:33:03+00:00

On my Asp.net core web api application, I'm query the database using ADO.Net and want to return the data table to the client, but it will show the 500 error with the following error message:

System.NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported. Path: $.Columns.DataType. System.NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported. at System.Text.Json.Serialization.Converters.UnsupportedTypeConverter1.Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) at System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)at System.Text.Json.Serialization.Metadata.JsonPropertyInfo1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)

Can someone help me solve this problem? Thx.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
2,604 questions
No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 18,886 Reputation points Microsoft Employee
    2023-01-25T03:48:14.6066667+00:00

    Hi @Jim Olewis

    From the official document, we can see that the System.Text.Json doesn't provide built-in support for the DataTable and related types.

    User's image

    So, you can try to use NewtonsoftJson package to serialization the data table in asp.net core application.

    Try to install the Microsoft.AspNetCore.Mvc.NewtonsoftJson via the NuGet package.

    Then, register the AddNewtonsoftJson() method in the program.cs file:

    builder.Services.AddControllers().AddNewtonsoftJson();
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    No comments

0 additional answers

Sort by: Most helpful