In my ASP.NET Core WebAPI controller, when I pass an exception object to JsonResult, it doesn't seem to recognize any of the exception properties and populate them accordingly (it just returns the 500 with no text). However, when I new-up an anonymous object and pass that in, it returns the 500 with what you would expect. It seems as though it has a big problem mapping an exception object directly and in its entirety. I'm wondering if there is a configuration issue/option.
[HttpGet]
public IActionResult GetSomething()
{
try
{
throw new Exception("hey");
}
catch (Exception e)
{
// the following does not work as expected
return new JsonResult(e)
{
StatusCode = StatusCodes.Status500InternalServerError
};
// the following works as expected
return new JsonResult(new { Message = e.Message })
{
StatusCode = StatusCodes.Status500InternalServerError
};
}
}