Developer technologies | ASP.NET | ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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?
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); ;
}