ASP.NET:自定义由 Page 驱动的 MasterPage 的最佳方式

发布于 2024-08-12 00:49:27 字数 951 浏览 5 评论 0原文

我的网站结构非常简单,只有一个主页和一堆页面。不过,母版页非常先进,我需要从页面中能够控制母版页的某些方面。

我希望能够在 aspx 文件中输入这些指令,以免使文件后面的代码混乱。

我的想法是创建不同的“指令”用户控件,例如 SeoDirective:

using System;

public partial class includes_SeoDirective : System.Web.UI.UserControl
{

    public string Title { get; set; }

    public string MetaDescription { get; set; }

    public string MetaKeywords { get; set; } 

}

我在需要覆盖默认 mastepage 设置的页面中包含此指令。

<offerta:SeoDirective runat="server" Title="About Us" MetaDescription="Helloworld"/>

在我的母版页中,我查看是否有任何指令:

includes_SeoDirective seo = (includes_SeoDirective) ContentPlaceHolder1.Controls.Children().FirstOrDefault(e => e is includes_SeoDirective);

(Children() 是一个扩展,因此我可以在 ControlCollection 上使用 Linq)

现在回答我的问题:我对此解决方案不太满意,可能有点臃肿?

我正在寻找替代解决方案,可以在 aspx 文件中创建这些标签。

我已经研究过扩展页面的技巧,但这需要我们修改 VS 配置才能编译项目,所以我放弃了该解决方案。

I have quite simple site structure with one mastepage and a bunch of pages. The mastepage is quite advanced though and I need from the page be able to control certain aspects of the Masterpage.

I want to be able to enter these directive in the aspx file to not clutter the code behind files.

My idea was to create different "directive" user controls such as SeoDirective:

using System;

public partial class includes_SeoDirective : System.Web.UI.UserControl
{

    public string Title { get; set; }

    public string MetaDescription { get; set; }

    public string MetaKeywords { get; set; } 

}

I include this directive in those pages that need to override the default mastepage settings.

<offerta:SeoDirective runat="server" Title="About Us" MetaDescription="Helloworld"/>

In my masterpage I look if there's any directives:

includes_SeoDirective seo = (includes_SeoDirective) ContentPlaceHolder1.Controls.Children().FirstOrDefault(e => e is includes_SeoDirective);

(Children() is an extension so I can work with Linq on a ControlCollection)

Now to my question: I'm not to happy about this solution might be a bit bloated?

I'm looking for alternative solutions where I can created these tags in the aspx file.

I've looked at the trick where I extend the Page, but that requiries we to modify the VS configs for the project to compile, so I dropped that solutions.

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

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

发布评论

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

评论(1

双马尾 2024-08-19 00:49:27

据我所知,没有标准的方法可以做到这一点。我过去以与您几乎相同的方式做过同样的事情,除了我在需要母版页查找的页面上使用了一个接口,它定义了一个可以调用的方法使用母版页执行特定逻辑。

您也许可以使用相同的范例:

ISpecialPage.cs:

public interface ISpecialPage
{
    string Title { get; set; }

    string MetaDescription { get; set; }

    string MetaKeywords { get; set; } 
}

MyPage.aspx:

public partial class MyPage : System.Web.UI.Page, ISpecialPage
{
    public string Title { get; set; }

    public string MetaDescription { get; set; }

    public string MetaKeywords { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {

        this.Title = "Some title";
        this.MetaDescription  = "Some description";
        this.MetaKeywords = "Some keywords";
    }
}

MasterPage.master:

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Context.Handler is ISpecialPage)
        {
            ISpecialPage specialPage = (ISpecialPage)this.Context.Handler;
            // Logic to read the properties from the ISpecialPage and apply them to the MasterPage here
        }
    }
}

这样您就可以处理母版页代码隐藏文件中的所有 MasterPage 逻辑,并且只需在需要提供某些功能的页面上使用该接口即可信息。

希望这对您有帮助!

As far as I am aware, there isn't a standard way of doing this. I have done this same thing in the past in much the same way you have, except I used an interface on the pages that I needed the master page to look for, which defined a method it could call to do specific logic with the master page.

You might be able to use this same paradigm:

ISpecialPage.cs:

public interface ISpecialPage
{
    string Title { get; set; }

    string MetaDescription { get; set; }

    string MetaKeywords { get; set; } 
}

MyPage.aspx:

public partial class MyPage : System.Web.UI.Page, ISpecialPage
{
    public string Title { get; set; }

    public string MetaDescription { get; set; }

    public string MetaKeywords { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {

        this.Title = "Some title";
        this.MetaDescription  = "Some description";
        this.MetaKeywords = "Some keywords";
    }
}

MasterPage.master:

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Context.Handler is ISpecialPage)
        {
            ISpecialPage specialPage = (ISpecialPage)this.Context.Handler;
            // Logic to read the properties from the ISpecialPage and apply them to the MasterPage here
        }
    }
}

This way you can handle all MasterPage logic in the master page code behind file, and simply use the interface on pages you need to provide certain information.

Hope this helps you!

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