title: How to avoid the circular referencing asp.net core mvc [HttpPost("add-recipe")] web api
body: <p>My project is Application with Recipes (cooking) .NET Core 5.0</p>
  <p>And i have problem with adding a new recipe (HttpPost) web api</p>
  <p>On postman my response is:</p>
  <p><em>&quot;A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles.&quot;</em></p>
  <p>When i'm creating a new recipe it should use recipeToCreateDto instead of Recipe - which contains all properties (circular referencing)</p>
  <p>Could you help me how to make it working properly. How to map etc.</p>
  <p><a href="https://i.postimg.cc/Mphv7zRH/screen.png" rel="nofollow noreferrer">https://i.postimg.cc/Mphv7zRH/screen.png</a>  &lt;- screen here</p>
  <p>I'm using AutoMapper for mapping classes and Repository Pattern.</p>
  <pre><code>public class AppUser
      {
          public int Id { get; set; }
          public string UserName { get; set; }
          public ICollection&lt;Recipe&gt; Recipes {get; set;} 
      }
  }
  </code></pre>
  <p>User has many recipes.</p>
  <pre><code> public class Recipe
      {
          public int Id { get; set; }
          public string Name { get; set; }
          public AppUser AppUser { get; set; }
          public int AppUserId { get; set; }
      }
  </code></pre>
  <p><strong>Data Transfer Object</strong></p>
  <pre><code>public class RecipeForCreateDto
      {
          [Required]
          [StringLength(50, MinimumLength = 3, ErrorMessage = &quot;You must specify name between 3 and 50 characters&quot;)]
          public string Name { get; set; }
          public int AppUserId { get; set; }
          public int AuthorId { get; set; }
      }
  </code></pre>
  <p>In my AutoMapperProfiles.cs</p>
  <pre><code>public class AutoMapperProfiles : Profile
      {
          public AutoMapperProfiles()
          {
          CreateMap&lt;RecipeForCreateDto, Recipe&gt;();
          }
  </code></pre>
  <p>Recipe Interface</p>
  <pre><code>  public interface IRecipeRepository
      {
        Task&lt;Recipe&gt; AddNewRecipe(Recipe recipe);
      }
  
      public class RecipeRepository : IRecipeRepository
      {
          private readonly DataContext _context;
          private readonly IMapper _autoMapper;
          public RecipeRepository(DataContext context, IMapper autoMapper)
          {
              _autoMapper = autoMapper;
              _context = context;
          }
  
   public async Task&lt;Recipe&gt; AddNewRecipe(Recipe recipe)
          {
              await _context.Recipes.AddAsync(recipe);
              await _context.SaveChangesAsync();
  
              return recipe;
          }
  </code></pre>
  <p>}</p>
  <p>Users Controller:</p>
  <p>User.GetUsername() is static method that is getting User's username.</p>
  <pre><code>[HttpPost(&quot;add-recipe&quot;)]
          public async Task&lt;ActionResult&gt; AddNewRecipe(RecipeForCreateDto recipeForCreateDto)
          {
              var userFromRepo = await _userRepository.GetUserByUsernameAsync(User.GetUsername());
  
              recipeForCreateDto.Name = recipeForCreateDto.Name.ToLower();
  
              if (await _recipeRepository.RecipeExists(recipeForCreateDto.Name))
                  return BadRequest(&quot;Recipe with that name already exists!&quot;);
  
              var recipeToCreate = _autoMapper.Map&lt;Recipe&gt;(recipeForCreateDto);
  
              recipeToCreate.AppUserId = userFromRepo.Id;
  
  
              var createdRecipe = await _recipeRepository.AddNewRecipe(recipeToCreate); // here is problem 
  
              var recipeToReturn = _autoMapper.Map&lt;RecipeForDetailDto&gt;(createdRecipe);
  
  
              return CreatedAtRoute(&quot;GetRecipe&quot;, new { controller = &quot;Recipes&quot;, id = createdRecipe.Id }, recipeToReturn);
          }
  </code></pre>