.Net Core 2.1 Web API returns a JSON but datatable cannot consume it. Why?
.Net Core 2.1 Web API returns a JSON but datatable cannot consume it. Why?
How come datatable cannot consume json returned from .Net Core 2.1 Web API?
Here is my .net code snippet.
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
[Produces("application/json")]
public IActionResult Get()
{
string s = @{ "data"": [[""Tiger Nixon"",""System Architect"", ""Edinburgh"", ""5421"",""2011/04/25"",""$320,800""],[""Garrett Winters"",""Accountant"",""Tokyo"", ""8422"",""2011/07/25"",""$170,750""]]}";
JObject.Parse(s).ToString(Newtonsoft.Json.Formatting.Indented);
return Ok(s);
}
What is wrong??? I've spent more than 4 hours trying to figure this out but to no avail. However when I access the web api in the browser it returns the json.
PLEASE HELP!!!!
Answers
There is quote after the @ but this forum is not showing it
Here is the updated code. But it's still NOT working.
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult Get()
{
It looks like you are returning a string - not JSON. You could decode that string into a JSON object on the client-side, but really you'd be better just sending plain JSON.
I'm no expert with .NET Newtonsoft controllers, but try:
Allan
Allan,
Thanks for the quick response. Still doesn't work.
Here are my server side snippet and html snippet. Any help will be greatly appreciated!
server side web api
client html
Hi @Sammy1 ,
Would you be able to link your page, or create a test case perhaps? Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin
If a test case isn't possible (hopefully it is!) then could you use debugger to give me a trace please - click the Upload button and then let me know what the debug code is.
Allan
I used fiddler and I see that it returns this.
c3
"{ \"data\" :[[ \"Tiger Nixon\",\"System Architect\", \"Edinburgh\", \"5421\",\"2011/04/25\",\"$320,800\"],[\"Garrett Winters\",\"Accountant\",\"Tokyo\", \"8422\",\"2011/07/25\",\"$170,750\"]] }"
0
from my web api from .net core 2.1
Hi @Sammy1 ,
It looks like it's being backslashed, which wouldn't be valid JSON. This thread should help, it's asking the same thing.
Cheers,
Colin
Ok, I got it fixed. Hope it will help out the next person who runs into the same problem.
This is for .net core web api
1. In Visual Studio, create the web api project
2. Add a class called DeChunkerMiddleware with the following content
/* Credit: Pharylon for providing the middleware to get rid of the response returning chunked data. Found this in the forum: https://stackoverflow.com/questions/37966039/disable-chunking-in-asp-net-core
*/
public class DeChunkerMiddleware
{
private readonly RequestDelegate _next;
...continue on next comment since this post only allows only certain length
...continue from previous comment
3) In the startup.cs, add the app.UseMiddleware<DeChunkerMiddleware>(); above app.UseMvc()
4) Add a class called, for example, Employee:
5) In the Controllers\ValuesController.cs
a. Inherit the class from Controller instead of ControllerBase (if you inherit from ControllerBase you will get Json not found error in the return statement when compiling.
b. you can use the default Get() method:
6) In the html file:
If you receive a CORS error you will need to add the following to the startup.cs in your .net core 2.1 (see the services.AddCors below, and
and in the controllers get method (applied locally) add the following attribute above the Get method
[EnableCors]
Excellent - thanks for posting your findings and good to hear you got it working.
Allan