Home > front end >  Unable to display DataTable in ASP.NET Core Web API
Unable to display DataTable in ASP.NET Core Web API

Time:10-10

I have to display a datatable in my ASP.NET Core Web API.

Here is my code.

Controller:

[Route("api/[controller]")]
[ApiController]
public class MapDataTableDemo : ControllerBase
{
    Product p;
    
    public MapDataTableDemo()
    {
        p = new Product();
    }
     
    [HttpGet]
    public async Task<IActionResult> MapDataTable()
    {
        var response = p.GetDataTable();
        return Ok(response);
    }
}

Product model class:

public class Product
{
    public DataTable dataTable { get; set; }

    public DataTable GetDataTable()
    {
        this.dataTable = new DataTable();
        this.dataTable.Columns.Add("product name");
        this.dataTable.Columns.Add("product price");
        this.dataTable.Rows.Add("mobile", 20000);
        this.dataTable.Rows.Add("tv", 15000);
        this.dataTable.Rows.Add("washing machine", 30000);
        return this.dataTable;
    }
}

When I debug the API, I can see the response but unable to display the datatable.

I get error as follows:

Error: response status is 500

Response body

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)
at System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter1.TryWriteAsObject(Utf8JsonWriter writer, Object value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.Converters.IEnumerableConverter1.OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonCollectionConverter`2.OnTryWrite(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, WriteStack& state)

Any solutions?

Thank you!

CodePudding user response:

Install the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package.

Add this line of code in Program.cs:

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

It worked for me.

  • Related