0

The following code results in a error show by the IDE in the return's lines. This code is: csharp(CS0103) El nombre 'StatusCode' no existe en el contexto actual [DatingApp.API]csharp(CS0103) The name 'StatusCode' does not exists in the current context

I'm using .NET 3.0 and the methods BadRequest and StatusCode seem to be correct and a part of the API.

Any clue of what could be going wrong?

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Core;
using DatingApp.API.Data;
using System.Threading.Tasks;
using DatingApp.API.Models;

namespace DatingApp.API.Controllers
{
    [Route("/api/[Controller]")]
    [ApiController]
    public class AuthController
    {
        private readonly IAuthRepository _repo;

        public AuthController(IAuthRepository repo)
        {
            _repo = repo;
        }

        [HttpPost("register")]
        public async Task<IActionResult> Register(string username, string password)
        {
            username = username.ToLower();

            if (await _repo.UserExists(username)){
                return BadRequest("Nombre de usuario ya existente");
            }
            var userToCreate = new User
            {
                Username = username
            };     

            var createdUser = await _repo.Register(userToCreate, password);

            return StatusCode(201);
        }
    }
}
heptagono
  • 69
  • 7
  • Has one topic for this anwer https://stackoverflow.com/questions/37690114/how-to-return-a-specific-status-code-and-no-contents-from-controller – Gustavo Mar 03 '20 at 19:56

2 Answers2

1

You need to inherit from ControllerBase in order to benefit. StatusCodeResult and OkObjectResult reside in ControllerBase. Hope this helps.

namespace DatingApp.API.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class AuthController : ControllerBase
    {
        private readonly IAuthRepository _AuthRepository;
Raymond
  • 131
  • 2
  • 9
0

AuthController needs to inherit from Controller in order for those methods to be available.

Jonathan Carroll
  • 880
  • 1
  • 5
  • 20