Json exception in core3.1

malathi p 6 Reputation points
2021-09-02T14:24:57.51+00:00

startup.cs configureservices method-

services.AddControllers().AddJsonOptions(x => x.JsonSerializerOptions.  = ReferenceHandler.Preserve);

sampcontroller.cs

[HttpGet]
        public JsonResult Get()
        {...
            return new JsonResult(table);
        }

Also NewtonsoftJson which version compatible with .net core 3.1?

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-09-03T02:46:19.043+00:00

    Hi @malathi p , I test your configuration in my newly created asp.net core 3.1 api project, and it worked in my side. Below is my configuration and code snippet. Could you pls compare it with yours to check the difference? And if it not work, could you pls share more details on your project and exception message?

    Add package:

    <ItemGroup>  
        <PackageReference Include="System.Text.Json" Version="5.0.2" />  
      </ItemGroup>  
    

    My test controller:

    using Microsoft.AspNetCore.Mvc;  
      
    namespace WebApplication2.Controllers  
    {  
        [Route("api/{controller}")]  
        public class HomeController : ControllerBase  
        {  
            [HttpGet]  
            public JsonResult Index()  
            {  
                var temp = new User  
                {  
                    user_id=1,  
                    user_name="test"  
                };  
                return new JsonResult(temp);  
            }  
        }  
    }  
    

    And in startup.cs I only changed ConfigureServices method like below:

    public void ConfigureServices(IServiceCollection services)  
            {  
                services.AddControllers().AddJsonOptions(x => x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve); ;  
            }  
    

    128876-image.png

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.