Reputation: 2360
I have this back-end system that produces json. Is it possible to pass that data through a controller on request and correctly setting the mimetype? Everything I've tried so far tries to re-serialize the data and thus escaping json string.
I've tried adding [Produces("application/json")]
to the controller. Setting the response type: Response.ContentType = MediaTypeNames.Application.Json;
, returning a JsonResult object: return new JsonResult(jsonString);
The back-end doesn't have a JsonSerializer I can add to the JsonSerializerOptions and I only need it for this specific controller.
In short: If I just return the json string as an IActionResult without doing anything the controller works except for the fact that the content-type isn't set to application/json. Is there any way to do this without affecting anything else?
I feel like this shouldn't be such a exceptional use case but I just can't seem to find the answer.
Upvotes: 1
Views: 136
Reputation: 33771
I had the same need. I solved it by having my action method return an ActionResult
like so:
public ActionResult SomeActionMethod(){
string json = GetJsonFromBackenend();
return new ContentResult {
Content = json,
ContentType = "application/json; charset=utf-8",
StatusCode = StatusCodes.Status200OK
};
}
You could declare the return type as an IActionResult
if you prefer as @Jurgy mentioned.
Upvotes: 1