1

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.

1
  • Hello! Would you be able to share the code that you had used? Commented Oct 13, 2020 at 13:59

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Using the interface IActionResult as return type works as well though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.