0

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.

4
  • 1
    Can't you simply use return Json(data)? Commented May 28, 2024 at 13:49
  • 1
    No but it think it works now, I just simplified the line into return data; firefox still highlights it and now chrome shows it too Commented May 28, 2024 at 13:56
  • 1
    @Peter why can't you use return Json? What you tried is wrong, hence the error. If you receive raw JSON from another service and want to return it use return Results.Text(data,"application/json",Encoding.UTF8) Commented May 28, 2024 at 14:04
  • 1
    Are you trying to forward the results of another API? In that case you may want to look at the YARP package and more specifically the Direct Forwarding scenario Commented May 28, 2024 at 14:05

1 Answer 1

1

If you want to return text with a specific content type use Results.Text() with the desired application type and encoding eg :

app.MapGet("/api/getLogs", async (HttpContext context) =>   
{
    data = await Data.GetLogs();
    return Results.Text(data, "application/json", Encoding.UTF8);
});
Sign up to request clarification or add additional context in comments.

5 Comments

Okay, well i already had found a solution that worked for me (in the comments of my question), and i know there are various ways your notation i had not yet seen though, Result.Text (i guess as in plain text) and then later say application.json intriguing. My answer in the comment below the question isnt wrong runs without errors. Though in this case unsure about whats good or what is wrong, is your answer better than just my old code ending with a simple "return data ;" ?
Yes, because return somestring will generate a JSON response consisting of a quoted string. If you try to return {"a":123} you'll get "{\"a\":123}" instead. You'll have to deserialize this twice to get an object with a property A whose value is 123
Ehm this was not in my case, as i mentioined: note data is a valid json formatted string
You misunderstood what I said. If data is a string, returning it will re-encode it as a standalone JSON string again. If you got something that isn't surrounded by " it means data is an object and can be returned as is with return data; or return Results.OK(data)
Ok thanks for the clarify i upvote and mark as answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.