How to create BasePage Model class in Razor Pages  ? RRS feed

  • Question

  • User-1042970710 posted

    In my application after authentication I store my User object in the session and move to another pages. If the session is null I will divert to Login page.

    Here is the Code where I check my Session

     public class IndexModel : PageModel
            public IActionResult OnGet()
                var usersLoginInfo = HttpContext.Session.GetObjectFromJson<UsersLoginInfo>("UserInfo");
                if (usersLoginInfo == null)
                    return new RedirectToPageResult("/Login");
                    return Page();

    What I was thinking to make separate class BasePageModel  inherited from PageModel  and then import all my pages from that class i.e BasePageModel  instead of PageModel.

    My problem is when I separate the the class and try to override the OnGet method The framework is complaining  that the method is not overridable.  

    I want to avoid checking my session in all my pages.... 

    Is there any work around?



    Friday, August 7, 2020 9:52 PM

All replies

  • User-474980206 posted

    to override OnGet you need to make it virtual. though I think you have picked a bad design. your authentication logic should be in middleware.

    Friday, August 7, 2020 11:45 PM