Home > Net >  Why I get this in JsonResult c#
Why I get this in JsonResult c#

Time:10-09

I'm using .NET 6 and an API using the Entity Framework. Scenario, I receive Soup API and then create new API, output is json result when I test I get correct results, but after I publish the project on Azure the API is not show out I want.

[HttpGet("[action]")]
        public IActionResult GetExamService(string id, string Examcodes, string codes)
        {
            try
            {
                ExamServiceClient exam = new();
                ExamDetailsStructure abilitiesExam = new();
                ExamResults.PersonNameBody ApplicantName = new();
                var response = exam.GetExamResultAsync(id, Examcodes, codes).Result.Item;
                if (!response.GetType().ToString().Equals(abilitiesExam.GetType().ToString()))
                {
                    throw new NullReferenceException("no data found "   id);
                }
                abilitiesExam = (ExamDetailsStructure)response;
                ApplicantName = (QyiasExamResults.PersonNameBody)abilitiesExam.ApplicantName.Item;

                ExamDetailsBody ExamDetailsBody = new()
                {
                    ExamSpecialty = abilitiesExam.ExamSpecialty.ToString() ?? "",
                    ExamResultTypeEnSpecified = abilitiesExam.ExamResult.ExamResultTypeEnSpecified.ToString() ?? "",
                };
                return Ok(new
                {
                    ExamDetailsBody = ExamDetailsBody
                });
            }
            catch (NullReferenceException ex)
            {
                ErrorResponse error = new(ex.Message, StatusCodes.Status404NotFound);
                return StatusCode((int)HttpStatusCode.NotFound, error);
            }
            catch
            {
                ErrorResponse error = new("internal server error", StatusCodes.Status500InternalServerError);
                return StatusCode((int)HttpStatusCode.InternalServerError, error);
            }
        }

JSON Output

the Azure output this results:

HTML Output

CodePudding user response:

This solved my problem. I used JSON Serializer NewtonsoftJson

    //JSON Serializer
builder.Services.AddControllers().AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore)
    .AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

for more details about package Microsoft Ignite and donwload from nuget Microsoft.AspNetCore.Mvc.NewtonsoftJson

  • Related