Categories

Tools

bitdefender

1&1 Web Hosting

How to add dinamically meta tag (title, description, keyword) to ASP.Net content page


The following example shows how to dynamically add meta tags (title, description, keywords) in the <head> an ASP.Net page.

Code:

using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;

public partial class info : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string title = GetTitle();
        string descriptionContent = GetDescription();
        string keywordsContent = GetKeywords();

        // Important! Overwrites the contents of the html tag <title>
        // Set the title of the page
        Page.Title = title;

        // Set meta tag description
        HtmlMeta htmlMetaDescription = new HtmlMeta();
        htmlMetaDescription.Name = "description";
        htmlMetaDescription.Content = descriptionContent;
        Header.Controls.Add(htmlMetaDescription);

        // Set meta tag keywords
        HtmlMeta htmlMetaKeywords = new HtmlMeta();
        htmlMetaKeywords.Name = "keywords";
        htmlMetaKeywords.Content = keywordsContent;
        Header.Controls.Add(htmlMetaKeywords);
    }

    /// <summary>
    /// Retrieves the title of the page, e.g. from the database.
    /// </summary>
    /// <returns></returns>
    private string GetTitle()
    { 
        string t = "Commercial information of the company MdmSoft";
        return t;
    }
    
    /// <summary>
    /// Retrieves a description of the page, e.g. from the database.
    /// </summary>
    /// <returns></returns>
    private string GetDescription()
    {
        string  d = "The MdmSoft Company, offers advice on computer systems in Small and Medium Enterprises.";
                d+= "Our web agency develops innovative websites.";
        return d;
    }

    /// <summary>
    /// Retrieves the keywords of the page, e.g. from the database.
    /// </summary>
    /// <returns></returns>
    private string GetKeywords()
    {
        string k = "MdmSoft, computer systems, Small and Medium Enterprises, web agency, innovative websites";
        return k;
    }
}
Posted in ASP.NET by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.