1

私のサイト構造は非常にシンプルで、1 つのマステページと多数のページがあります。ただし、マスターページは非常に高度であり、ページからマスターページの特定の側面を制御できる必要があります。

これらのディレクティブを 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; } 

}

デフォルトのマステページ設定をオーバーライドする必要があるページには、このディレクティブを含めます。

<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構成を変更する必要があるため、そのソリューションを削除しました。

4

1 に答える 1

1

私の知る限り、これを行う標準的な方法はありません。私は過去にあなたとほぼ同じ方法でこれと同じことをしましたがinterface、マスターページで特定のロジックを実行するために呼び出すことができるメソッドを定義した、マスターページを探す必要があるページで を使用しました。

これと同じパラダイムを使用できる場合があります。

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 ロジックを処理し、特定の情報を提供するために必要なページでインターフェイスを使用するだけです。

これがお役に立てば幸いです!

于 2009-11-25T09:46:28.717 に答える