Is it possible to return self-created JSON from an controller action method, so that it is not additionally escaped by ASP Net Core?
Consider the following code:
[Route("api/[controller]")]
[ApiController]
[Produces("application/json")]
public class MyController : Controller
{
[HttpPost]
public async Task<ActionResult<string>> Post(ItemPostRequest itemPostRequest)
{
var result = await callStoredProcedureOnDatabase(itemPostRequest); // generates JSON directly on the database using SQL Server's FOR JSON clause
return CreatedAtAction(nameof(GetItem),
new { Id = result.Id },
result.Json
);
}
}
- If I remove the third line with
[Produces("application/json")]I get the unescaped JSON as expected, but the Content-Type header is only set totext/plain. - If I leave the
[Produces("application/json")]in place, the Content-Type is correctly set toapplication/json, but my JSON become escaped.
So, how can I return the result.Json without getting escaped while having a correct Content-Type header of application/json?