最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Inherited custom PageModel, possible to redirect in custom PageModel? - Stack Overflow

programmeradmin2浏览0评论

For my layout, there are some variables I want to pass in for the navbar and what have you (such as username). So for that, I created a LayoutModel class that inherits PageModel.

public class LayoutModel : PageModel
{
  public NavbarDataModel Navbar { get; set; }
  public readonly FeaturesConfiguration _config;

  public LayoutModel(IHttpContextAccessor httpContextAccessor, IOptionsSnapshot<FeaturesConfiguration> options) : base()
  {
    Navbar = new NavbarDataModel();
    Navbar.UserName = httpContextAccessor.HttpContext.Session.GetString("username");

    if (string.IsNullOrEmpty(Navbar.UserName))
    {
        RedirectToPage("Login"); // does not redirect as it isn't returning anything, but intended behavior
    }

    _config = options.Value;
  }
}

The redirect doesn't work, I can create an IActionResult OnGet() to return a redirect, but unfortunately, that conflicts with the OnGet() of the inheriting page causing the application to blow up. Is there a way to do this check in the parent model so that I don't have to perform this check in all the inheriting ones?

For my layout, there are some variables I want to pass in for the navbar and what have you (such as username). So for that, I created a LayoutModel class that inherits PageModel.

public class LayoutModel : PageModel
{
  public NavbarDataModel Navbar { get; set; }
  public readonly FeaturesConfiguration _config;

  public LayoutModel(IHttpContextAccessor httpContextAccessor, IOptionsSnapshot<FeaturesConfiguration> options) : base()
  {
    Navbar = new NavbarDataModel();
    Navbar.UserName = httpContextAccessor.HttpContext.Session.GetString("username");

    if (string.IsNullOrEmpty(Navbar.UserName))
    {
        RedirectToPage("Login"); // does not redirect as it isn't returning anything, but intended behavior
    }

    _config = options.Value;
  }
}

The redirect doesn't work, I can create an IActionResult OnGet() to return a redirect, but unfortunately, that conflicts with the OnGet() of the inheriting page causing the application to blow up. Is there a way to do this check in the parent model so that I don't have to perform this check in all the inheriting ones?

Share Improve this question asked Mar 17 at 19:25 RobertRobert 1,7945 gold badges34 silver badges63 bronze badges 1
  • Have you looked into Filters and overriding them? Pl see this link for help: learnrazorpages/razor-pages/…. You can do something like :public override void OnPageHandlerExecuting(PageHandlerSelectedContext context) { if(context.HandlerMethod.MethodInfo.Name == nameof(OnGet)) { // code placed here will only execute if the OnGet() method has been selected } } – SoftwareDveloper Commented Mar 17 at 22:14
Add a comment  | 

1 Answer 1

Reset to default 0

You could use RedirectToPageResult like below:

    public class LayoutModel : PageModel
    {
        public NavbarDataModel Navbar { get; set; }
        public readonly FeaturesConfiguration _config;
        private readonly IHttpContextAccessor _httpContextAccessor;

        public LayoutModel(IHttpContextAccessor httpContextAccessor, IOptionsSnapshot<FeaturesConfiguration> options) : base()
        {
            this._httpContextAccessor = httpContextAccessor;
            _config = options.Value;
        }

        public override void OnPageHandlerExecuting(PageHandlerExecutingContext context)
        {

            Navbar = new NavbarDataModel();
            Navbar.UserName = _httpContextAccessor.HttpContext.Session.GetString("username");

            if (string.IsNullOrEmpty(Navbar.UserName))
            {
                context.Result = new RedirectToPageResult("/Login");
                return;
            }
            base.OnPageHandlerExecuting(context);
        }
    }

Then inherit like

    public class IndexModel : LayoutModel
    {
        public IndexModel(IHttpContextAccessor httpContextAccessor, IOptionsSnapshot<FeaturesConfiguration> options)
            : base(httpContextAccessor, options) { }
        public void OnGet()
        {
        }
    }
发布评论

评论列表(0)

  1. 暂无评论