1

When returning null value, following code in Web API controller executed successfully without any errors, while debugging it. But in browser it gives Status Code: 500 Internal Server Error. Please see the attached error image.

[HttpGet("GetPreRegisteredPatientByMrn/{patientId}")]
public async Task<PreRegistrationPatientDetailsDto> GetPreRegisteredPatientByMrn(int patientId)
{
      try
      {
        return await _readService.GetPreRegisteredPatientByMrn(patientId);
      }
      catch (Exception ex)
      {
        _logger.LogError(ex.StackTrace);
        throw;
      }
}

StatusCode-500

PS - Earlier this code written in .netcore version 2.1.Later updated to .netcore 3.1 version. Internal server error 500 comes in .net core 3.1 version. it works fine with .netcore 2.1 version.

Aslam Jiffry
  • 1,306
  • 6
  • 23
  • 57
  • 1
    Looks like a serialization issue - Can you try this method - https://stackoverflow.com/a/46956001/38024 and check what exception is throwing? – Anuraj Feb 05 '22 at 15:53
  • services.AddControllers().AddNewtonsoftJson(options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; options.SerializerSettings.Formatting = Formatting.Indented; options.SerializerSettings.Error = (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args) => { Console.WriteLine(args.ErrorContext.Error.Message); Console.WriteLine(args.ErrorContext.Error.StackTrace); }; }); – Aslam Jiffry Feb 05 '22 at 16:12
  • @Anuraj , Since using .netcore 3.1 , my code is looks like above comment. But when debugging breakpoint never hits into that error handling code. – Aslam Jiffry Feb 05 '22 at 16:13

1 Answers1

1

it's hard to know whats going on, but it could be failing to convert the dto to a json/xml object. Maybe you can catch the exception if you tinker with the exception settings and turn the CLR exception on.

enter image description here

Skittles86
  • 96
  • 1
  • 4