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