简体   繁体   English

如何在任务中使用<actionresult<ienumerable<t> &gt;&gt; </actionresult<ienumerable<t>

[英]How can I use in Task<ActionResult<IEnumerable<T>>>

I have this function ("GetUsers") in my API:我的 API 中有这个函数(“GetUsers”):

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".我试图从登录功能调用它:(UserConstants 只是一个示例类,现在我想通过调用“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谢谢你

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.这不仅仅是一个方法,而是一个控制器动作( 阅读更多),通常不应手动调用,而是由框架(ASP.NET/ASP.NET Core)调用并由其处理返回值。

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".我试图从登录功能调用它:(UserConstants 只是一个示例类,现在我想通过调用“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).你不应该,出于多种原因,首先,当你需要找到一个用户时,获取所有用户是没有意义的,只需编写正确的查询(也可能你不应该按原样将密码存储在数据库中,但应该考虑存储密码哈希 -请参阅此问题),次要操作可以具有更多与Authenticate无关的逻辑(例如NotFound似乎不是有效响应,而且_context.Users不应为null ,如果_context是 EF 上下文)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以(或应该)使用 IAsyncEnumerable<T> 而不是任务<ActionResult<IEnumerable<T> &gt;&gt; 在 Web API 控制器中 - Can (or should) I use IAsyncEnumerable<T> instead of Task<ActionResult<IEnumerable<T>>> in a Web API Controller 如何使用 OnExceptionAspect 返回 ActionResult <IEnumerable<Model> &gt; - How do I use OnExceptionAspect to return ActionResult<IEnumerable<Model>> 我怎样才能“适应”一个任务<ienumerable<t> &gt; 到 IAsyncEnumerable<t> ? </t></ienumerable<t> - How can I "adapt" a Task<IEnumerable<T>> to IAsyncEnumerable<T>? 如何从任务转换<generic.list<t> &gt; 到一个 Generic.IEnumerable<t> ? </t></generic.list<t> - How can I convert from a Task<Generic.List<T>> to a Generic.IEnumerable<T>? 我不能在.ascx上使用IEnumerable()吗? - Can't I use IEnumerable() on .ascx? 如何返回ActionResult? - How can I return an ActionResult? 的ActionResult <IEnumerable<T> &gt;必须返回一个List <T> - ActionResult<IEnumerable<T>> has to return a List<T> 无法决定任务 <IActionResult> ,IActionResult和ActionResult <Thing> - Can't decide between Task<IActionResult>, IActionResult and ActionResult<Thing> 无法隐式转换类型“IEnumerable<t> '到'动作结果<ienumerable<t> >' </ienumerable<t></t> - Cannot implicitly convert type 'IEnumerable<T>' to 'ActionResult<IEnumerable<T>>' 如何将IEnumerable <Task <T >>转换为IObservable <T> - How to convert an IEnumerable<Task<T>> to IObservable<T>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM