Asp.net identity System.NotSupportedException: Serialization and deserialization of 'System.Action' instances are not supported

Asjad Butt 71 Reputation points
2022-11-22T14:13:56.95+00:00

Hi so im using the method FindUserById tfrom asp.net identity and i get this error ill attach the code

**

  • Controller method

**

[HttpGet("{id}")]  
        public IActionResult GetUser(string id)  
        {  
            var user = userManager.FindByNameAsync(id);  
            return Ok(user);  
        }  

**

  • AppUser Entity

**

 public class AppUser : IdentityUser<Guid>  
    {  
        public override string UserName { get; set; }  
        public string? FatherName { get; set; }  
        public int? BranchId { get; set; }   
        public Branch? Branch { get; set; }  
        public ICollection<AppUserRole> UserRoles { get; set; }  
   }  

The user is already registered and can be seen in sql the guid is there etc etc ill attch that to

263123-image.png

Also ill attach the error

263151-image.png

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,158 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,253 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,201 Reputation points
    2022-11-22T15:43:12.32+00:00

    The action method should be async and you're missing an await.

            [HttpGet("{id}")]  
            public async Task<IActionResult> GetUserAsync(string id)  
            {  
                var user = await userManager.FindByNameAsync(id);  
                return Ok(user);  
            }  
    

    I recommend learning async/await programming in C#.

    Asynchronous programming with async and await

    I also recommend not using "var" until you have a better understanding of C# types are being returned. Hover your cursor over "user" and select "Use explicit type instead of 'var'" from the tip. Doing this step will show the actual type returned from the method which will help verify you're working with the expected type.

    263106-capture.png

    Keep in mind, you might receive a different error after making the action async since the design returns an entity. The Json serializer will attempt to serialize every relationship. You might have to add the following to the program.cs file.

    builder.Services.AddControllers().AddJsonOptions(x =>  
                    x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);  
    

    A better solution is returning a POCO class otherwise known as a View Model.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Michael Taylor 47,966 Reputation points
    2022-11-22T15:37:02.273+00:00

    That means the User object has a function action in it. What does the Branch type look like?

    0 comments No comments