2.5: Add DTO and AuthController
Add UserForRegister DTO
To register, a user will enter their username and password. They obviously won't enter their ID stored on our database, so we'll need a data transfer object (DTO) to only send the username and password.
In your .Models
project - add a folder called User
.
Inside of that folder, add a class called UserForRegister.cs
.
public class UserForRegister
{
public string Username { get; set; }
public string Password { get; set; }
}
Add AuthController
Inside of our .API
project - create a new class called AuthController.cs
inside of the Controllers
folder.
public class AuthController : Controller
{
}
First, we'll identify the route we'd like to use for this controller:
[Route("api/[controller]")]
public class AuthController : Controller
{
}
Next, we'll write our constructor.
The interesting thing to note here is that we don't have to tell our constructor about the concrete implementation of our IAuthService
interface.
The controller will accept anything that implements the interface - we just have to register what we want to use in the ConfigureService() method in our Startup class - and our 'container' will inject what is needed.
Dependency Injection is hopefully starting to click!
[Route("api/[controller]")]
public class AuthController : Controller
{
public AuthController(IAuthService authService)
{
}
}
As we've done before - ctrl + . on authService and choose "Initialize field from parameter."
[Route("api/[controller]")]
public class AuthController : Controller
{
private readonly IAuthService _authService;
public AuthController(IAuthService authService)
{
_authService = authService;
}
}
Last updated