In my asp.net webapi project I need to return string json data as a file. What is also important I want to have status code of the response set to 200(successful). I found many examples where people use MemoryStream and FileStreamResult to get proper file from the server but it doesn't work for me.
Unfortunately I always get message in the browser which is "Unexpected error", although the serwer code works without any exceptions. I checked details of the request in a browser and it has status code "Bad request" (400).
My code:
[HttpGet()]
public async Task<IActionResult> Download()
{
string jsonData = "example data";
byte[] byteArray = Encoding.UTF8.GetBytes(jsonData);
var stream = new MemoryStream(byteArray);
var result = new FileStreamResult(stream, new MediaTypeHeaderValue("text/json"))
{
FileDownloadName = "testFile.json"
};
return result;
}