Overview
-Blazor Server project using REST API.
-HttpClient
calls my API using PostJsonAsync<>
-Db is updated successfully and returns expected JSON
-API Controller Try/Catch does not throw exception, executes return CreatedAtAction()
-Output shows API returns 500: Internal Server Error
-When executing POST using Swagger, same line is executed and returns 201
I'm at a loss for what is going wrong. I've only been coding since July, so I'm hoping it's just my lack of experience that's blinding me to the obvious problem in my code. This is the offending method:
public async Task<ActionResult<PlayerCharacter>> CreatePlayerCharacter(PlayerCharacter playerCharacter)
{
try
{
if (playerCharacter != null)
{
var newPlayerCharacter = await _playerCharacterRepository.AddPlayerCharacter(playerCharacter);
//This is the last line of code to execute before the web app crashes with Status Code 500. Executes fine and returns 201 from Swagger.
return CreatedAtAction(nameof(GetPlayerCharacter), new { id = newPlayerCharacter.Id }, newPlayerCharacter);
}
return BadRequest();
}
catch (Exception e)
{
//This break point is not reached
Console.WriteLine(e.StackTrace);
return StatusCode(StatusCodes.Status500InternalServerError, "Error posting data to the database.");
}
}