1

I have this function ("GetUsers") in my API:

private readonly DBCyberCaseTools _context;

public UserController(DBCyberCaseTools context)
{
    _context = context;
}

// GET: api/User
[HttpGet]
public async Task<ActionResult<IEnumerable<User>>> GetUsers()
{
  if (_context.Users == null)
  {
      return NotFound();
  }
    return await _context.Users.ToListAsync();
}

But I don't understand how to use it from another function?

When I call this function, I can't understand how I access the result?

(in swagger it works well)

I'm trying to call it, from function of Login: (UserConstants is just an example class, and now I would like to replace this by calling to "GetUsers".

private UserModel Authenticate(UserLogin userLogin)
{
    var currentUser = UserConstants.Users.FirstOrDefault(o => o.Username.ToLower() == userLogin.Username.ToLower() && o.Password == userLogin.Password);

    if (currentUser != null)
    {
        return currentUser;
    }

    return null;
}

Thank You

prog_prog
  • 371
  • 2
  • 5
  • 15
  • 1
    Why would you be directly calling a Controller Action method from elsewhere in your ASP.Net code? Can you give an example of what you're trying to do, and what explicit issue you're facing? – gunr2171 Dec 19 '22 at 19:04
  • I edited my question with more details. Thanks. – prog_prog Dec 19 '22 at 19:16

1 Answers1

1

This is not just a method, but a controller action (read more here), which usually should not be called manually but is invoked by framework (ASP.NET/ASP.NET Core) and the return value is processed by it.

The only "standard" reason to call this method directly I know is for unit testing, in this case you need to await the result of the method and process it contents:

var controller = ...; // instantiate the controller
ActionResult<IEnumerable<User>> actionResult = await controller.GetUsers();
IEnumerable<User>? users = actionResult.Value; // check users
ActionResult? result = actionResult.Result; // check set result for example: result is NotFoundResult

I'm trying to call it, from function of Login: (UserConstants is just an example class, and now I would like to replace this by calling to "GetUsers".

You should not, for multiple reasons, first of all there is no point in fetching all users when you need to find one, just write correct query (also possibly you should not store passwords in database as is, but should consider storing password hashes - see this question), secondary - action can have much more logic which is not relevant for Authenticate (for example NotFound does not seem to be a valid response, also _context.Users should not be null if _context is EF context).

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • Thank you about your answer. So what do you recommend me in my case (I edited my qestion below)? I have "userController" - that connected to DB, and retrive the information from DB. and I have also "LoginController" - for Authentication. and I would like to call to userController from LoginController. – prog_prog Dec 19 '22 at 19:17
  • Thanks for your extensive explanation. Could you perhaps direct me to a good example for the correct construction of what I need? I understand that I built everything not quite right. – prog_prog Dec 19 '22 at 19:24
  • 1
    @prog_prog from your explanation it does not seem that there is common logic shared between `GetUsers` and `Authenticate`, so you should not call former from latter, just inject context into `LoginController` and use it. – Guru Stron Dec 19 '22 at 19:25
  • 1
    @prog_prog I would argue that you have enough to implement `LoginController`. There are several tutorials in [the official docs](https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-7.0&tabs=visual-studio) but I'm not sure that there is one that can be matched one-to-one to your case. – Guru Stron Dec 19 '22 at 19:27
  • 1
    Okay. I think I understood the main points. I will try to implement. Thank you! – prog_prog Dec 19 '22 at 19:28