call today Call Today 320.281.0605

asp.net dynamic meta and title tags

July 1, 2009

Using a Master page.

Problem for SEO is it really scews up the formatting and positioning of the tags.

First get the data there.

If you’re not overly concerned about formatting of the title tag, in the content page:

Page.Title = “The title of your page”;

// Add the keywords meta tag
HtmlMeta metaKeywords = new HtmlMeta();
metaKeywords.Name = "keywords";
metaKeywords.Content = nseCase.MetaKeywords;
Page.Header.Controls.AddAt(1, metaKeywords);
Page.Header.Controls.AddAt(1, new LiteralControl("\n"));

// Add the description meta tag
HtmlMeta metaDescription = new HtmlMeta();
metaDescription.Name = "description";
metaDescription.Content = nseCase.Description;               
Page.Header.Controls.AddAt(1,metaDescription);
Page.Header.Controls.AddAt(1, new LiteralControl("\n"));

What is happening is the meta tags are being added to the first line in the head tag (below the title tag). Think of it like a stack, that is why it is being done in reverse order in which you would think.

If you want to do a little more work to remove the spacing and line breaks from the title tag you need to take this approach.

In the Master page remove the runat=”server” from the head tag. Now you can create literal tags for each desired tag in the <head> tag.

<head>
<asp:Literal ID=”litTitle” runat=”server” />
<asp:Literal ID=”litMetaDescription” runat=”server” />
<asp:Literal ID=”litMetaKeywords” runat=”server” />
</head>

Now in your content page you can access these literal controls by:

Literal title = (Literal)Master.FindControl("litTitle");
title.Text = String.Format("<title>{0}</title>", “Your dynamic title”);

Literal metaDescription = (Literal)Master.FindControl(“litMetaDescription”);
metaDescription.Text = String.format(“<meta name=\”{0}\” content=\”{1}\” />”, “description”, “here is your page description”);

Literal metaKeywords = (Literal)Master.FindControl(“litMetaKeywords”);
metaKeywords.Text = String.format(“<meta name=\”{0}\” content=\”{1}\” />”, “keywords”, “here, are, your, pages, keywords”);

The first approach is quicker to do and still allows you to put a default title in it. You may not want to set the title and meta tags for each page.

The second approach requires that you set the title and meta tags on every page if you want them. I suppose you could use a contentplaceholder for many of the pages which is quicker than doing it in the code-behind for pages that you don’t need the tags to be dynamic.

My SEO guy is finally happy that I came up with a solution for this.  I hope this helps you get an SEO guy off your back. :)

  • Share/Bookmark
blog comments powered by Disqus
Agent Cody Banks 2 Destination London full lenght movie download Afghan Knights download movie Inside the Smiths download movie Beverly Hills Chihuahua download movie Permanent Vacation download movie Indiana Jones and the Kingdom of the Crystal Skull download movie Afghan Knights download movie Inside the Smiths download movie Beverly Hills Chihuahua download movie Permanent Vacation download movie Indiana Jones and the Kingdom of the Crystal Skull download movie