We usually 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.
From SEO(Search engine optimization) point of view Meta tags like Meta Description, Meta Keywords must be unique and relevant to content of the page. So it is better to add unique and relevant Meta tags on the Page Load event on the each content page.
From SEO(Search engine optimization) point of view Meta tags like Meta Description, Meta Keywords must be unique and relevant to content of the page. So it is better to add unique and relevant Meta tags on the Page Load event on the each content page.
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)
using System.Web.UI.HtmlControls;
protected void Page_Load(object sender, EventArgs e)
{
//adding Page Title dynamically
Page.Title = "Add Page Title";
//adding
meta description dynamically
HtmlMeta metaDesc
= new HtmlMeta();
metaDesc.Name = "description";
metaDesc.Content = "Write Meta Description
here";
Page.Header.Controls.Add(metaDesc);
//adding meta keywords dynamically
HtmlMeta metakeyWord
= new HtmlMeta();
metakeyWord.Name = "keywords";
metakeyWord.Content = "Write 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
imports System.Web.UI.HtmlControls
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
'adding Page Title dynamically
Page.Title = "Add Page Title"
'adding meta description dynamically
Dim metaDesc As New HtmlMeta()
metaDesc.Name = "description"
metaDesc.Content = "Write Meta Description
here"
Page.Header.Controls.Add(metaDesc)
'adding meta keywords dynamically
Dim metakeyWord As New HtmlMeta()
metakeyWord.Name = "keywords"
metakeyWord.Content = "Write Meta Keywords
here"
Page.Header.Controls.Add(metakeyWord)
End Sub
0 comments:
Post a Comment