How to add meta tags for a web page dynamically

If you are using some Content Management System (CMS) for your web site, then there is good chance that all your content along with meta tag content like Title, Description, Keywords etc. is stored in some data source and need to be added to the page dynamically. This all is doable from server side using HtmlMeta html server side control to Controls collection of Header of your page. You just need to add head element to your page and make sure that you have set runat=server for it and you are good to go. See the following snippet how it is done.


protected void Page_Load(object sender, EventArgs e)
{
 if (!IsPostBack)
 {
  this.Header.Title = "My Page Title";
  HtmlMeta meta = new HtmlMeta();
  meta.Content = "my description for this page";
  meta.Name = "description";
  this.Header.Controls.Add(meta);
 }
}

And on the page I have defined the mark up like this.


<head runat="server">
    <title></title>
</head>

Search

Social

Weather

26.9 °C / 80.4 °F

weather conditions Clear

Monthly Posts

Blog Tags