简体   繁体   中英

Base Controller inheritance in ASP.NET Core identity

I just started my first project in asp net core and I migrate from .Net framework.

I created a new project using ASP.NET Core 2.1 and I added Identity via

Right click on project -> Add -> Add scaffolded items

to my project.

First of all something make me confused. All of the files for identification moved to an area called Identity and Manage section files moved to Account folder. Most important thing is I don't have the account and manage controllers

I created a new empty controller called BaseController :

public class BaseController : Controller
{
    private ApplicationDbContext _db { get; set; }

    public BaseController(ApplicationDbContext db)
    {
        _db = db;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (HttpContext.User.Identity.IsAuthenticated)
        {
            string userEmail = User.Identity.Name;                
            ViewBag.CurrentUser = _db.Users.Where(u => u.Email.Equals(userEmail)).FirstOrDefault();
        }

        base.OnActionExecuting(filterContext);
    }
}

In this controller I get the logged in user and pass it to a ViewBag and show it in my view.

I have a HomeController that inherits from BaseController :

public class HomeController : BaseController
{
    private UserManager<ApplicationUser> _userManager { get; set; }
    private readonly ApplicationDbContext _db;

    public HomeController(UserManager<ApplicationUser> userManager, 
                          ApplicationDbContext db) : base (db)
    {
        _userManager = userManager;
        _db = db;
    }

    public IActionResult Index()
    {
        return View();
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
}

This part of code works fine but when I try to navigate to Manage/Index or Manage/ChangePassword I get this error and I don't know how can I resolve this problem. The cause of this error is ViewBag is empty because the base controller action never run.

Sorry if my question is simple and is not professional

In asp.net core 2.1, scaffold Identity uses Razor Pages rather than MVC structure, so there is no controllers in Identity area and you could not inherit controller. Refer to here .

Since it is in area, you need to use url like https://localhost:44367/Identity/Account/Manage/Index or https://localhost:44367/Identity/Account/Manage/ChangePassword to access the Razor Pages.

Besides, ViewBag is not supported in Razor Pages, you could use ViewData instead, refer to here .

For adding filters for Razor Pages, you could refer to Filters in Razor Pages

public class BasePageModel : PageModel
{
    public override void OnPageHandlerExecuting(PageHandlerExecutingContext context)
    {
        //...
    }
}
public class IndexModel : BasePageModel
{
    public void OnGet()
    {
        //...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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