Adding Dynamic Meta Tags with Master Pages in ASP.NET using C #

Today each and every website is getting dynamic, every website for example have just on page and is being used to display the contents for various categories, hence the need for the dynamic meta title, meta description and meta keywords arises especially for implementing Search Engine Optimization(SEO) properly on your website having dynamic pages.

In a website what people do is create a Master Page and then this Master Page further contains a number of content or child pages, now what we want to have is having separate meta title, meta description and meta keywords for every child page, but here is the problem as once you run you run your website – preference is given to the meta title, meta description and meta keywords you have given in you Master Page and hence all the child pages will have same meta title, description and keywords.

Basically In ASP.NET you do not have any direct function to re write the title,description and keywords when you are working with Master Pages. To solve this problem I have written some sample code:

Firstly in your master page find this code

< head runat=”server”>
< title>Untitled Page< /title>

Than Replace it with the following code :

< head runat=”server” id=”master_Head”>
< meta name=”description” runat=”server” id=”master_Description” content=”” />
< meta name=”keywords” runat=”server” id=”master_keywords” content=”” />
< title>Untitled Page< /title>

That’s all you have to change in your master page.
Now in your child or content page include one Header File:

using System.Web.UI.HtmlControls;

Once all this done, now in your child or content page , on load write following code:

void Page_Load(Object sender, EventArgs e)
    HtmlHead objHead = new HtmlHead();
    objHead = (HtmlHead)Master.FindControl(“master_Head”);
    HtmlMeta objMetaDescription = (HtmlMeta)objHead.FindControl(“master_Description”);
    HtmlMeta objMetaKeywords = (HtmlMeta)objHead.FindControl(“master_keywords”);

    Title = “[Write You Title Here]”;
    objMetaDescription.Content = “[Write You Description Here]”;
    objMetaKeywords.Content = “[Write You Keywords Here]”;


That’s All.

Note: You will have to write the above mentioned code in all your child pages, where you want to set Dynamic Meta Title Description And Keywords

4 thoughts on “Adding Dynamic Meta Tags with Master Pages in ASP.NET using C #”

  1. Hi
    Nice article…
    Wanna know that in dynamic website meta tag and meta description column in admin page will be provided by web developer or not. My web developer is saying that dynamic website don’t meta tags. please reply ASAP.
    Thanks in advance.

    1. Hi Swati
      Thanx for appreciating the article…

      Yes you are right in dynamic website , web developer should provide an option for adding and editing meta tag and meta description in admin panel .
      Without having an option in admin panel, you won’t be able to set dynamic meta tags,description for any of your pages in your dynamic website.

      Let me know if u need any more help..

  2. How can we add meta tag according to product when we have only single (ex: productdetail.aspx) page i want to display seperate meta tags for diffrent products…

Comments are closed.