C# ASP.NET MasterPage 嵌套继承是邪恶的

发布于 2024-09-26 10:09:55 字数 2053 浏览 2 评论 0 原文

我有一个包含相当多不同的 MasterPages / BasePages 的解决方案,它们都从其他地方继承。我的问题是我在BaseMaster中有一个虚拟字符串,它被BaseManagement覆盖,但是当我尝试访问这个字符串时,我总是得到基值

继承母版和页面的目的显然是为了避免到处都有重复的代码。

也许这与我在每个主机上“需要”protected void Page_Load(object sender, EventArgs e)这一事实有关,无论


Base Classes

所以,这是来自 Base.Master.cs 的一个片段

public abstract partial class BaseMaster : MasterPage, IRewritablePageElement
{
    public BasePage BasePage { get { return Page as BasePage; } }
    public BaseMaster Self { get { return (BaseMaster)this.Page.Master; } }
    public virtual string accessUri { get { return "/"; } }
    public string AccessUri { get { return Self.accessUri; } }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.OnPageLoad(sender, e);
    }

    /// <summary>
    /// If we want to do something on page load, we override the following method
    /// </summary>
    public virtual void OnPageLoad(object sender, EventArgs e)
    {
        SetCacheability();

        RedirectSiteDown();
        RedirectUnregisteredUsers();
        RedirectUnprivilegedUsers();

        if(!IsPostBack)
        {
            PopulateMenu();

            ...

,正如您已经注意到的那样,

    public BaseMaster Self { get { return (BaseMaster)this.Page.Master; } }
    public virtual string accessUri { get { return "/"; } }
    public string AccessUri { get { return Self.accessUri; } }

它很丑陋,因为它应该是这样,

    public virtual string AccessUri { get { return "/"; } }

但代码不知何故设法得到一直下降到 AccessUri 为“/”的级别,即使它已在更高级别的某处被“/a/”覆盖:

public partial class BaseManagementMaster : BaseMaster
{
    public override string accessUri { get { return "/a/"; } }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

如果不是 Self 的东西,这里 AccessUri 仍然是“ /”。这完全没有意义。 另外,我必须重新声明 Page_Load 方法这一事实也没有什么意义。

是否有一种干净的方法来进行母版页继承,这实际上是可能的吗?

I have a solution with quite a few different MasterPages / BasePages which all inherit from somewhere else. My problem is that I have a virtual string in BaseMaster, which is overridden by BaseManagement, but when I try to access this string I always get the base value

The point of inheriting masters and pages is obviously to avoid having duplicate code everywhere.

Maybe it has something to do with the fact that I "need" protected void Page_Load(object sender, EventArgs e) at every master no matter

Base Classes

So, here's an snippet from Base.Master.cs

public abstract partial class BaseMaster : MasterPage, IRewritablePageElement
{
    public BasePage BasePage { get { return Page as BasePage; } }
    public BaseMaster Self { get { return (BaseMaster)this.Page.Master; } }
    public virtual string accessUri { get { return "/"; } }
    public string AccessUri { get { return Self.accessUri; } }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.OnPageLoad(sender, e);
    }

    /// <summary>
    /// If we want to do something on page load, we override the following method
    /// </summary>
    public virtual void OnPageLoad(object sender, EventArgs e)
    {
        SetCacheability();

        RedirectSiteDown();
        RedirectUnregisteredUsers();
        RedirectUnprivilegedUsers();

        if(!IsPostBack)
        {
            PopulateMenu();

            ...

As you already noticed,

    public BaseMaster Self { get { return (BaseMaster)this.Page.Master; } }
    public virtual string accessUri { get { return "/"; } }
    public string AccessUri { get { return Self.accessUri; } }

is as ugly as it gets, since it should just be

    public virtual string AccessUri { get { return "/"; } }

but the code somehow manages to get all the way down to a level where AccessUri is "/" even though it has been overriden with "/a/" somewhere at a higher level:

public partial class BaseManagementMaster : BaseMaster
{
    public override string accessUri { get { return "/a/"; } }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

If not for the Self thingy, here AccessUri is still "/". which makes absolutely no sense.
Also, the fact that I have to re-declare Page_Load methods makes very little sense too.

Is there a clean way to do master page inheritance, is it even realistically possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两人的回忆 2024-10-03 10:09:55

问题在于像

BaseManagementMaster : BaseMaster

这样继承母版页是错误的,正确的方法应该是只声明母版页,然后在页面级别而不是母版级别进行抽象。

the problem was in inheriting masterpages like

BaseManagementMaster : BaseMaster

this is wrong, the correct way to do this should be to just declare MasterPages, and then have abstractions at Page level, not Master level.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文