为 ApplicationUser.cs 添加控制器? MVC核心

Adding a controller for ApplicationUser.cs? MVC Core

我正在尝试为 ApplicationUser.cs 添加一个控制器,以便当管理员登录时,他们能够查看、编辑、删除 dbo.AspNetUsers table 但是我认为我做错了。有什么想法吗?

这是 ApplicationUser.cs 的代码:

namespace ProjectTest.Models
{
    // Add profile data for application users by adding properties to the ApplicationUser class
    public class ApplicationUser : IdentityUser
    {
        public string Summary { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        override public string UserName { get; set; }
        public string RoleType { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        override public string PhoneNumber { get; set; }
        override public string Email { get; set; }
    }
}

当我尝试为 ApplicationUser 模型添加控制器时 class,控制器已创建(包括视图文件)但无法启动。控制器是新建控制器后的默认代码

Dean,你完全走上了正轨。 但是,我将向您介绍如何手动创建控制器,以及如何创建编辑方法。请随时检查您生成的控制器是否存在任何不匹配,或者更好的是,完全按照这些步骤操作。

首先:我建议您创建一个 Viewmodel(在一个新文件中),它只包含您打算向管理员公开的属性。此视图模型可能如下所示。

public class UserViewModel
{    
    public string Summary { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string RoleType { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    override public string PhoneNumber { get; set; }
}

请注意,使用视图模型是一种更标准化的方法,因此请跳过此步骤。

下一步:您应该像这样创建一个控制器,例如 AdminController

[Authorize(Roles = "Admin")]
public class AdminController : Controller
{
     private readonly ApplicationDbContext _dataContext;
     private readonly UserManager<ApplicationUser> _userManager;

    public AdminController(ApplicationDbContext dataContext, UserManager<ApplicationUser> userManager, IHostingEnvironment environment)
    {
        // Inject the datacontext and userManager Dependencies
        _dataContext = dataContext;
        _userManager = userManager;
    }

    // HTTPGET Controller action to edit user
    [HttpGet]
    public IActionResult EditUser()
    {
        return View();
    }

     // HTTPPOST Controller action to edit user
    [HttpPost]
    public async Task<IActionResult> EditUser(UserViewModel model)
    {
        //Get User by the Email passed in.
        /*It's better practice to find user by the Id, (without exposing the id to the view).
        However, to keep this example simple, we can find the user by email instead*/
        var user = await _userManager.FindByEmailAsync(model.Email);

        //edit user: replace values of UserViewModel properties 
        user.Summary = model.Summary;
        user.FirstName = model.FirstName;
        user.LastName = model.LastName;
        user.RoleType = model.RoleType;
        user.Address = model.Address;
        user.City = model.City;

         //add user to the datacontext (database) and save changes
        _dataContext.Update(user);
        await _dataContext.SaveChangesAsync();

        return RedirectToAction("ACTION_NAME", "CONTROLLER_NAME");
        //this could be
        //return RedirectToAction("Index", "Home");
    }
}