I am trying to get data from database using ASP.NET CORE API the problem that I can't convert the datatable to JSON I used this solution but i got the same problem.
This is what I get every time I test with Postman
"[{\"id_auxiliaire\":\"0000000008522450131\",\"identite_fiscal_cin\":\"0XDERTTOL45\",\"NOM_RAISON_SOCIALE\":\"HIGHTech\",\"CodePostale\":41225,\"Ville\":\"USA\"},}]"
This is my WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Configure Web API to return JSON
config.Formatters.JsonFormatter
.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("text/html"));
}
}
And this is my code
SqlDataAdapter da = new SqlDataAdapter($"select * from [UserDetail] WHERE CONVERT(date, dateOperation) BETWEEN '{dd}' AND '{df}' ", con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
string res = JsonConvert.SerializeObject(dt, Formatting.None);
return new string[] { res };
}