0

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. Strange XHR

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; }
    }
Pepperized
  • 317
  • 2
  • 10
  • Your code seems OK. Can you make a [mcve](https://stackoverflow.com/help/minimal-reproducible-example)? – Dmitry Apr 05 '20 at 23:03
  • What do you mean by "only works ocasionally"? What is it doing and what are you expecting? – Jonathan Alfaro Apr 05 '20 at 23:15
  • @Dmitry I'll do that shortly. – Pepperized Apr 05 '20 at 23:18
  • @JonathanAlfaro Sometimes I get the expected result, some Json in the body, but mostly it's an empty Json content – Pepperized Apr 05 '20 at 23:19
  • You still have not explained what you expect what you are getting... Please post both Json responses so we can see what you are seeing. Also what is _lobbiers.InsertOne?? – Jonathan Alfaro Apr 05 '20 at 23:53
  • also what are you using to make the requests? and why are you using from query if your action uses POST? The reason i ask all these questions is because I want to reproduce the problem on my end – Jonathan Alfaro Apr 05 '20 at 23:55
  • Your controller is probably failing. You can see this post to at least get a good response including the error details: https://stackoverflow.com/questions/19379168/ignore-exceptions-caused-by-missing-controller-parameters – Oguz Ozgul Apr 06 '20 at 00:44
  • @JonathanAlfaro I have ammended the question with the Json responses. _lobbies.InsertOne() inserts a document into a MongoDB database. I decided to use POST because the action is creating something. I am using Axios on a seperate frontend to make the Http Request. – Pepperized Apr 06 '20 at 08:11

1 Answers1

0

Really bizzare behaviour.. but it was CORS. Once I added CORS to my Startup.cs it worked.

It remains a mystery why there was no exceptions, no warnings in the browser, a response of OK from the server and even sometimes the correct body being given.

Pepperized
  • 317
  • 2
  • 10
  • 2
    Not bizzarre and that's no solution. You didn't say you were trying to make the call from a different origin. Disabling CORS protection is no fix - in fact, disabling HTTPS will get your site flagged by every browser. Add the expected origins, methods etc instead. Otherwise your application will be open to attacks – Panagiotis Kanavos Apr 06 '20 at 10:16
  • Very true, changed the answer to remove the snippet so as to not tempt users to copy it. I meant only to illustrate that CORS was the issue. – Pepperized Apr 06 '20 at 10:48