Adding Meta tags to Content pages having a common Master page in ASP.Net 2.0 || Setting Meta tags programatically on an ASP.Net page


The problem:

ASP.Net 2.0 introduced master pages which promotes HTML Markup re usability among several Content pages. While working with Content pages I found that you cannot set the Meta tags for the page from the HTML markup. I did some research and found the following solution of setting the meta tags of a page programatically. I hope the solution will be helpful to developers struggling to set Meta tags for content pages and people trying to perform SEO on content pages that use a master page.


The solution:

As shown below, add the following code inside the Page_Load method of the content page. Please set the Title, Meta Description and Meta Keywords tags to the appropriate .Net string values of your preference.

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

//Adding Meta tags to the Content Page ASP.Net 2.0

HtmlHead header = (HtmlHead)Page.Header;

if (header != null)

{

//header.Title - will set the Title of the page

header.Title = @"Cachinko - Where career networking really pays";

HtmlMeta metaDescription = new HtmlMeta();

metaDescription.Name = "description";

//metaDescription.Content - This is where the description // will go as a .Net String

metaDescription.Content = @"Cachinko is a career networking tool that helps Employers network securely with their employees and reduce recruiting costs by efficiently utilizing the Employee Referral Program.";

header.Controls.Add(metaDescription);

HtmlMeta metaKeywords = new HtmlMeta();

metaKeywords.Name = "keywords";

//metaKeywords.Content - This is where the comma seperated // keywords would go as a .Net String

metaKeywords.Content = @"Cachinko, Career Networking, Social Networking";

header.Controls.Add(metaKeywords);

}

}

}

This is how it will render in the HTML Page

<html>

<head>

<title>Cachinko - Where career networking really paystitle>

<meta name="description" content="Cachinko is a career networking tool that helps Employers network securely with their employees and reduce recruiting costs by efficiently utilizing the Employee Referral Program." />

<meta name="keywords" content="Cachinko, Career Networking, Social Networking " />

head>

<body>

<form name="form1" method="post" action="Default.aspx" id="form1">

form>

body>

html>

Comments

  1. very interesting. SEO developers and marketers will certainly appreciate.

    ReplyDelete

Post a Comment

Popular posts from this blog

WPF How to Dispose ViewModel when the associated UserControl (Not Window) closes?

C# How to unit test Dispatcher

WPF: How to Deep Copy WPF object (e.g. UIElement) ?