I wrote a Minimal Web API with this code:
app.MapGet("/api/getLogs", async (HttpContext context) =>
{
context.Response.Headers.ContentType = "application/json";
data = await Data.GetLogs(); // note data is a valid json formatted string
return context.Response.WriteAsync(data);
});
As above it shows perfectly with Firefox, the json is color highlighted numbers and strings get recognized. I feel happy how Firefox does it.
Though Chrome won't show it...
When browsing to this API endpoint, I see a developer error in Visual Studio:
System.InvalidOperationException: Headers are read-only, response has already started.
But without that context.Response line the Firefox highlighting of the json isn't shown so Firefox does receives json, despite the warning, it works perfectly ).
I tried several other ways like, just showing some below:
context.Response.WriteAsync(await Data.GetLogs() )); }
and
app.MapGet("/", (HttpContext context) => context.Response.WriteAsJsonAsync(new { Message = Data.GetLogs() }));
etc
Sometimes it worked in Chrome, and sometimes it works in Firefox, but to get it with json highlighting only works as in my first example and only for Firefox.
I like to be able to see the highlighting as in Netscape (I don't mind to have another browser for just this). But in a perfect world I rather have it so that Firefox shows it with highlighting and Chrome, however Chrome can show it (current code Chrome doesn't show it at all).
Note also that it is just this API call that should work this way, my other request are different.
return Json(data)?return Json? What you tried is wrong, hence the error. If you receive raw JSON from another service and want to return it usereturn Results.Text(data,"application/json",Encoding.UTF8)