简体   繁体   English

如何在 ASP.NET 内核中包含 object 的 ApplicationUser

[英]How to include the ApplicationUser of an object in ASP.NET Core

I'm trying to make an app just like LinkedIn / Facebook.我正在尝试制作一个类似于 LinkedIn / Facebook 的应用程序。

I have a list of posts and each one has an ApplicationUser (ASP.NET IdentityUser ).我有一个帖子列表,每个帖子都有一个ApplicationUser (ASP.NET IdentityUser )。

In my PostController , I want to return the list of posts and include the ApplicationUser .在我的PostController中,我想返回帖子列表并包含ApplicationUser

But I don't want every attribute of the user as an object containing a (hashed) password.但我不希望用户的每个属性都作为包含(散列)密码的 object 。

How can I return a post including ApplicationUser but without the password attribute?如何返回包含ApplicationUser但没有密码属性的帖子?

Post.cs

public class Post
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string ApplicationUserID { get; set; }
    [MaxLength(500)]
    public string Contents { get; set; }
    [Required]
    public ulong Timestamp { get; set; }
}

PostController : PostController

[Authorize]
[HttpGet("{id}")]
public async Task<ActionResult<Post>> GetPost(int id)
{
    var post = await _context.Posts
                             .Include(x=> x.ApplicationUser)
                             .FirstOrDefaultAsync(x => x.Id == id);

    if (post == null)
    {
        return NotFound();
    }

    return post;
}

ApplicationUser : ApplicationUser

public class ApplicationUser : IdentityUser
{
}

You can create a view model which included the required properties, such as below:您可以创建一个包含所需属性的视图 model,如下所示:

public class PostViewModel
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string ApplicationUserID { get; set; }
    [MaxLength(500)]
    public string Contents { get; set; }
    [Required]
    public ulong Timestamp { get; set; }

    public string UserName { get; set; } //Directly add the username property, used to store the user name

    public UserViewModel User { get; set; } //create a new view model which contains the username
}

public class UserViewModel
{
    public string UserName { get; set; }
}

Then, change the LINQ statement as as below:然后,将 LINQ 语句更改如下:

var query = _dbcontext.Posts.Include(c => c.ApplicationUser)
               .Select(c => new PostViewModel()
               {
                   Id = c.Id,
                   Contents = c.Contents,
                   Timestamp = c.Timestamp,
                   UserName = c.ApplicationUser.UserName,
                   User = new UserViewModel()
                   {
                       UserName = c.ApplicationUser.UserName
                   } 
               }).ToList();

The result as below:结果如下:

在此处输入图像描述

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

相关问题 在ASP.NET Core MVC中注入ApplicationUser - Inject ApplicationUser in ASP.NET Core MVC ASP.NET MVC:如何正确地将自定义对象引用到ApplicationUser? - ASP.NET MVC: How to properly reference custom object to ApplicationUser? ASP.NET Core 标识用户管理器<ApplicationUser> -Error 没有为此对象定义无参数构造函数 - ASP.NET Core Identity UserManager<ApplicationUser> -Error No parameterless constructor defined for this object ApplicationUser上的ASP.net Core 1.0映射属性返回null - ASP.net Core 1.0 Mapping property on ApplicationUser returning null ApplicationUser 始终为空 ASP.NET CORE 3.0 标识 - ApplicationUser always null ASP.NET CORE 3.0 Identity ASP.NET Core Identity不会注入UserManager <ApplicationUser> - ASP.NET Core Identity does not inject UserManager<ApplicationUser> Asp.Net Core 从 ApplicationUser 派生的不同用户模型 - Asp.Net Core Different User Models to derive from ApplicationUser 我应该如何从ASP.NET核心视图访问我的ApplicationUser属性? - How should I access my ApplicationUser properties from ASP.NET Core Views? ASP.NET 核心 - 如何将 IdentityDbContext 中的 ApplicationUser id 插入到 Employee Model 作为 UserId - ASP.NET Core - How to insert ApplicationUser id in IdentityDbContext into Employee Model as UserId 如何从 ASP.net 内核 6 中的视图访问自定义 ApplicationUser 属性? - How to access custom ApplicationUser properties from view in ASP.net core 6?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM