1

私のサイトの3つの異なるページに3つの異なるいいねボタンがあります。ボタンがどのページにあるかに基づいて、これらのogタグを動的に作成して割り当てる方法はありますか?

これが私が使っているコードです:

protected void Page_Load(object sender, EventArgs e)
{


    // ADD META INFORMATION TO HEADER
    HtmlHead head = (HtmlHead)Page.Header;


    // KEYWORDS
    HtmlMeta hm = new HtmlMeta();
    hm.Name = "keywords";
    hm.Content = this.metaKeywords;
    head.Controls.Add(hm);

    // DESCRIPTION
    hm = new HtmlMeta();
    hm.Name = "description";
    hm.Content = this.metaDescription;
    head.Controls.Add(hm);

    // ************************************************************************

    //      <meta property="og:title" content="Faces of Metastatic Breast Cancer (MBC) Video Wall" />
    //      <meta property="og:type" content="cause" />
    //      <meta property="og:image" content="http://www.facesofmbc.org/images/MBC_Logo.png"/>
    //      <meta property="og:url" content="http://bit.ly/rkRwzx" />
    //      <meta property="og:site_name" content="Faces of Metastatic Breast Cancer (MBC)" />
    //      <meta property="og:description" content="I just viewed the new Faces of Metastatic Breast Cancer (MBC) video wall. For each view, comment or share of the video wall during October, Genentech will donate $1 to MBC initiatives. Watch TODAY!" />
    //      <meta property="fb:admins" content="653936690"/>

    string ogTitle = "";
    string ogType = "";
    string ogImage = "";
    string ogUrl = "";
    string ogSiteName = "";
    string ogDescription = "";
    string ogAdmins = "";


    if (Page.Request.Path.Contains("videoWall.aspx"))
    {
        hm = new HtmlMeta();
        hm.Attributes.Add("property", "og:title");
        hm.Content = "TEST OG TITLE";
        head.Controls.Add(hm);

        hm = new HtmlMeta();
        hm.Name = "og:type";
        hm.Content = "TEST OG TYPE";
        head.Controls.Add(hm);
    }
    // ************************************************************************
}

私はそれが間違っていて、さまざまなアプローチがあるように見えることを知っていますが、私が何をしていたか、そして私が向かっていた方向を示しているだけです。コメントアウトされたタグは、私が必要とするタグのガイドとしてそこにあります。どんな助けでもいただければ幸いです!

前もって感謝します!

4

1 に答える 1

2

プロパティをインターフェイスに変換し、そのインターフェイスをコントロールを使用するクラスにスローするとどうなるでしょうか?

取得しようとしているすべてのプロパティを含むインターフェイスを作成できます...

interface IFaceBookMeta
{
    string ogTitle {get; set;}
    string ogType {get; set;}
    //...... and so on
}

次に、そのインターフェイスを、コントロールをホストするページに適用します。

public partial class SomePageThatHasTheControl: System.Web.UI.Page, IFaceBookMeta

次に、そのクラスで、インターフェイスのプロパティを明示的に設定できるようになりました

this.ogTitle = "A Random Title";
this.ogType = "A Type";

次に、コントロールのコードに移動して、次のようにします。

//This is the page that is hosting the control.
IFaceBookMeta meta = (IFaceBookMeta)this.Page;

hm = new HtmlMeta();
hm.Attributes.Add("property", "og:title");
hm.Content = meta.ogTitle;
head.Controls.Add(hm);

hm = new HtmlMeta();
hm.Name = "og:type";
hm.Content = meta.ogType;
head.Controls.Add(hm);
//.... and so on

このようにすると、コントロールを別のページに追加するたびにコントロールのコードを変更できなくなります。代わりに、コントロールが必要なページでインターフェイスを平手打ちし、プロパティを設定するだけです。

于 2012-09-28T17:14:59.993 に答える