开发者

Sitefinity - Set Title and Description Programmatically

Anyone know how to set the Title and Description of a page programmatically?

I've implemented this solution from Telerik:

public class InternalPageCustom : InternalPage
{
开发者_高级运维    public InternalPageCustom()
    {
    }

    protected override void SetTitle(Telerik.Cms.ICmsPage page)
    {
        //base.SetTitle(page);
        this.Title = "somevalue";
    }
}

The only problem is that the base object InternalPage only provides the property Title, nothing for other meta fields like description or keywords?

Regards, Jacques


Currently we do this as late as possible in the page to ensure that the can be sure especially for the title tag that we can replace or append the title if need be.

    protected override void OnPreRender(EventArgs e)
    {
        var cmsPage = this.Page as CmsPageBase;
        if (cmsPage != null)
        {
            cmsPage.Title = "My Title";
            cmsPage.Header.Controls.Add(new LiteralControl("<meta name=\"keywords\" content=\"my key words" /><meta name=\"description\" content=\"My description\" />"));   
        }

        base.OnPreRender(e);
    }

The only problem with this approach is that if someone fills in any Description or Keywords in the Sitefinity backend then this will add both. Hoever the above could be extended to check the Header.Controls collection for those tags and remove/replace them.


I have solution for you, that you needn't to implement"InternalPageCustom", just using code Inline in your master page.

Ex:

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["id"] == null)
    {
        return;
    }

    var id = Request.QueryString["id"];

    var pageTitle = string.Empty;
    var pageDescription = string.Empty;
    var pageKeyword = string.Empty;

    var propertyId = Guid.Parse(id);
    if (propertyId != Guid.Empty)
    {
        const string culture = "en";

        //Method use for get dynamic title, Description and Keyword.
        PropertyHelper.SetPageInfo(propertyId, culture, out pageTitle, out pageDescription, out pageKeyword);
    }

    Page.Title = pageTitle;
    Page.MetaDescription = pageDescription;
    Page.MetaKeywords = pageKeyword;
}

==> Push this code to your Master page. Hope this help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜