简体   繁体   English

&#39;任务<User> &#39; 不包含 &#39;LastActive&#39; 的定义

[英]'Task<User>' does not contain a definition for 'LastActive'

Here's my .cs Code, where the error is being generated (line 23):'Task' does not contain a definition for 'LastActive' and no accessible extension method 'LastActive' accepting a first argument of type 'Task' could be found这是我的 .cs 代码,其中正在生成错误(第 23 行):“Task”不包含“LastActive”的定义,并且无法找到接受“Task”类型的第一个参数的可访问扩展方法“LastActive”

using System.Security.Claims;
using System.Threading.Tasks;
using DatingApp.Data;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using DatingApp.Models;
namespace DatingApp.Helper
{
    public class LogUserActivity : IAsyncActionFilter
    {
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var resultContext = await next();

            var userId = int.Parse(resultContext.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var repo = resultContext.HttpContext.RequestServices
                .GetService<IUserRepository>();

            var user = repo.GetUserById(userId);

            user.LastActive= DateTime.Now;
            await repo.SaveAll();
        }
    }
}

Here is my repository这是我的存储库

 public async Task<User> GetUserById(int id)
        {
            var user =await  _context.Users.Include(p=>p.Photos).FirstOrDefaultAsync(u=>u.Id==id);
            return user;
        }

GetUserById doesn't return a User , it returns a Task<User> . GetUserById不返回User ,它返回Task<User> You need to await it to get the User :您需要等待它来获取User

var user = await repo.GetUserById(userId);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM