Please disable your adblock and script blockers to view this page
 

How to add meta description,meta keywords tags from code behind in asp.net(C#, VB)

Introduction: In previous articles i explained How to get current page URL/Address in asp.net and How to get browser name,version,type, operating system and How to Convert Rupees,Currency or Numbers to Words and Get age in years,months,days,hours and seconds from DOB and Get city, state and country based on zip code using Google map API and How to enable JavaScript.  In this article i will explain How to add meta description,meta keywords tags from code behind in asp.net(C#, VB). 

Description: We all know how to add Meta tags like Meta Description, Meta Keywords in the <HEAD> tag. But sometimes it is required to add Meta tags like Meta Description, Meta Keywords etc dynamically from code behind file. E.g. in case of Content pages where there is no <Head> tag and if we add those Meta tags in the Master page then they will be same for all the Content pages. 

But for good SEO(Search engine optimization) Meta tags like Meta Description, Meta Keywords must be unique and relevant to content of the page.  So the solution is to add unique and relevant Meta tags on the Page Load event on the each content pages as demonstrated in the code below:

Implementation: Let's check it out by an example

C#.NET Code to add meta description,meta keywords tags from code behind
  • In the code behind file(.aspx.cs) write  the code on Page load event as: 
First include the following required namespace:

using System.Web.UI.HtmlControls;

protected void Page_Load(object sender, EventArgs e)
        {
            //adding Page Title dynamically
            Page.Title = "write your page Title here";
            //adding meta description dynamically
            HtmlMeta metaDesc = new HtmlMeta();
            metaDesc.Name = "description";
            metaDesc.Content = "Write your Meta Description here";
            Page.Header.Controls.Add(metaDesc);

            //adding meta keywords dynamically
            HtmlMeta metakeyWord = new HtmlMeta();
            metakeyWord.Name = "keywords";
            metakeyWord.Content = "Write your Meta Keywords here";
            Page.Header.Controls.Add(metakeyWord);
        }

VB.NET Code to add meta description,meta keywords tags from code behind
  • In the code behind file(.aspx.vb) write  the code on Page load event as: 
First import the following required namespace:

imports System.Web.UI.HtmlControls

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        'adding Page Title dynamically
        Page.Title = "write your page Title here"

        'adding meta description dynamically
        Dim metaDesc As New HtmlMeta()
        metaDesc.Name = "description"
        metaDesc.Content = "Write your Meta Description here"
        Page.Header.Controls.Add(metaDesc)

        'adding meta keywords dynamically
        Dim metakeyWord As New HtmlMeta()
        metakeyWord.Name = "keywords"
        metakeyWord.Content = "Write your Meta Keywords here"
        Page.Header.Controls.Add(metakeyWord)
End Sub

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."

Previous
Next Post »

2 comments

Click here for comments
Anonymous
admin
February 19, 2014 ×

This is a great article. I really appreciate your help with this!

Reply
avatar
February 19, 2014 ×

Hi ..thanks for your feedback..it is always nice to hear that my article helped anyone..stay connected and keep reading for more useful updates..

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon