I'm making a .Net Core 3.1 API and I'm having unreliable responses. Most of the time they are blank and very occasionally works as expected. I am using NewtonsoftJson and have added .AddNewtonsoftJson(); to the appropriate places in Startup.cs. No exceptions and a response of Ok for each.
Here is the full code of the action.
[HttpPost("/lobby/new")]
public IActionResult NewLobby([FromQuery] string name)
{
string playerGUID = Guid.NewGuid().ToString();
var lobby = new Models.Lobby
{
LobbyMembers = new List<Models.Lobby.LobbyMember>
{
new Models.Lobby.LobbyMember
{
PlayerID = playerGUID,
Name = name
}
},
HostID = playerGUID,
State = Models.Lobby.LobbyState.Open
};
_lobbies.InsertOne(lobby);
var response = new Models.JsonOut.LobbyInfo
{
LobbyID = lobby.MongoID,
PlayerID = playerGUID
};
return Json(response);
}
Here is an example of an expected and working body result:
{"playerID":"7183f34b-3524-45d0-a760-bcf62b1f4313","lobbyID":"5e8ae31b844735202ceb62c3"}
The unexpected result is 0B of Json in the body of the response.
Strangely it is working occasionally rather than not at all. Here is my network tab of Firefox developer tools. Highlighted are correct.
UPDATE: I made a mcve and the issue persists. I created a ASP.NET WebApi project with .NET Core 3.1 and disabled Configure for HTTPS.
public class TestController : Controller
{
[HttpPost("/lobby/new")]
public IActionResult NewLobby([FromQuery] string name)
{
string playerGUID = Guid.NewGuid().ToString();
var response = new Models.JsonOut.LobbyInfo
{
LobbyID = "test",
PlayerID = playerGUID
};
return Json(response);
}
}
I'll also attach the class being returned.
public class LobbyInfo
{
[JsonProperty("playerID")]
public string PlayerID { get; set; }
[JsonProperty("lobbyID")]
public string LobbyID { get; set; }
}