Add or Remove Meta Tags - May 2011

If you download this tip please consider making a modest donation.

Search engines use meta tag information to create a site index. Some of the web applications I've created are accessed via different URLs. So I needed a method to dynamically alter the meta tags for the search engines. I searched the web and found several suggested solutions. I used some of the suggested solutions and encapsulated them into a VB.Net class module called cAddRemoveMetaTags. The class is described here. It becomes the May 2011 Tip-of-the-Month.

I published the application on the web. Click here to run the demo application.

The Application

VS/2008 project AddRemoveMetaTags accepts meta tag values then when the 'do it' button is clicked it stores those values into its document.

Meta Tag values and options. See below for details.
Enter values or select options then click the 'Do Update Meta Tags' button.

Right mouse-click on the page then select 'View source'.

The viewed source.
Click here to run the demo application.

Methods and Properties

Methods and properties. If a new meta tag is entered and an old one exists then the old one is removed.

AddMetaTags()   Add or remove the meta tags. Returns True.
RemoveMetaTag(oPage As Page, _
As String)
  If it exists, remove meta tag sName form oPage.
Returns True if successful.
Properties Default Comment
.oPage Nothing Point to the page to be modified.
An error is generated if this property is not specified.
.sTitle Empty Enter the page title.
.sDescription Empty Enter a description of the page.
.sKeywords Empty Enter keywords separated by commas.
.sCopyright Empty Enter copyright information
.sAuthor Empty Enter the authors name.
.sGoogleBot "index, follow" Enter GoogleBot values. Click here for details.
Robots is set to this value as well.
.sGoogleSiteVerification Empty Enter the Google Site Verification key if applicable.
Click here for details.
.bRemoveRevisitTag True If True then the 'revisit-after' tag is removed if it exists.
There is a lot of discussion on the web about this meta tag.
Most agree that it is not used by search engines.
.bRemoveRobots False If True then GoogleBot and Robots are not inserted
and if they exist they are removed.
.bNoIndex False If True then both GoogleBot and Robots are set to
'NoIndex, NoFollow'.
.bRemoveDescriptionAndKeywords False If True the Description and Keywords tags are not
inserted and if they exist they are removed.

The RemoveMetaTag Method

This is the complete RemoveTag method. Click here to see the C# code that inspired this method.

Private Function RemoveMetaTag(ByVal oPage As Page, ByVal sName As String) As Boolean
    For i As Integer = 1 To oPage.Header.Controls.Count
        If TypeOf (oPage.Header.Controls(i - 1)) Is HtmlMeta Then
oHtmlMeta As HtmlMeta = oPage.Header.Controls(i - 1)
If oHtmlMeta.Name.ToUpper = sName.ToUpper Then
oPage.Header.Controls.RemoveAt(i - 1)
Return True

Download the VB.Net Code

Download the source code by clicking on the zipped file name. Click here to run the demo application.

Download - Source Zipped File Size
VS/2008 VB.Net, ASP.Net Solution 54K

Previous Tip    Tip Index    Next Tip


Updated 2011/05/10