Official guides

Performance

ASP Core Web API Example

[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
  protected readonly IUsersRepository _repository;

  public UsersController(IUsersRepository repository)
  {
      _repository = repository;
  }

  [HttpGet]
  [ProducesResponseType(200)]
  public async Task<ActionResult<IEnumerable<User>>> GetAllAsync()
  {
      return await _repository.GetAll();
  }        

  [HttpGet("{username}")]
  [ProducesResponseType(200)]
  [ProducesResponseType(404)]
  public async Task<ActionResult<User>> GetByIdAsync(string username)
  {
      var existingUser = await _repository.GetByUsername(username);
      if (existingUser == null)
      {
          return NotFound();
      }
      return existingUser;
  }

  [HttpPost]
  [ProducesResponseType(201)]
  [ProducesResponseType(400)]
  public async Task<ActionResult<User>> CreateUserAsync(User user)
  {
      await _repository.AddUser(user);
      return CreatedAtAction(nameof(GetById), new { id = user.Username }, user);
  }

  [HttpPut("{username}")]
  [ProducesResponseType(204)]
  [ProducesResponseType(404)]
  public async Task<IActionResult> UpdateUserAsync(string username, User user)
  {
      var existingUser = await _repository.GetByUsername(username);
      if (existingUser == null)
      {
          return NotFound();
      }

      existingUser.Nickname = user.Nickname;
      existingUser.Email = user.Email;

      _repository.Update(existingUser);
      return NoContent();
  }

  [HttpDelete("{username}")]
  [ProducesResponseType(204)]
  [ProducesResponseType(404)]
  public async Task<IActionResult> DeleteUserAsync(string username)
  {
      var existingUser = await _repository.GetByUsername(username);
      if (existingUser == null)
      {
          return NotFound();
      }

      _repository.Delete(existingUser);
      return NoContent();
  }
}