Dynamic Meta Tags With ASP.NET And C#
One of the advantages of using ASP.NET for web development and design is its template pages. Basically you use a single template page for a website and then add content pages which first load the overall template and then incorporate their own content to render a single web page. This is a very useful and convenient way of producing sites, and makes changes to the overall site structure and coding easy through the modification of the template page from which all other pages derive.
The static HTML of the <head> section of the web page is part of the template page. And the <head> section of a web page is what contains the <meta> tags which provide important structured metadata about a web page. This data is used by browsers rendering the site and also by search engines that are indexing the site. And while some of the metadata about web pages in a single site might be consistent (e.g. the language or author information), some attribute such as keywords, description or robots are likely to vary from page to page.
If the <meta> tags are simply added in static HTML to the template page, however, they will be constant over all pages that reference that template. This may not be appropriate and indeed can have a negative impact on search engine optimisation if a search engine webcrawler indexes a page and logs no relation between the listed keywords attribute and the actual text content of the page. So what we want to be able to do is maintain the stability and flexibility offered by an overall template page but still add dynamic <meta> tags that vary between different content pages.
I code using ASP.NET with C# and it’s pretty easy to introduce dynamic meta tags to the content pages through the C# code.
Coding in Visual Studio with ASP.NET and C#, each page will have a pagename.aspx and a pagename.aspx.cs file, which are compiled on the web server when rendering the page. We’ll be making changes to the .cs page and the C# code.
Every page has the following;
protected void Page_Load(object sender, EventArgs e)
{
}
…and we are simply going to add some code-behind to the content pages that will add the <meta> tags for that particular page. In this example, we’re going to add the description and keyword tags.
protected void Page_Load(object sender, EventArgs e)
{
HtmlMeta desc = new HtmlMeta();
desc.Name = “description”;
desc.Content = “My description for this page”;
Header.Controls.Add(desc);
HtmlMeta keyw = new HtmlMeta();
keyw.Name = “keywords”;
keyw.Content = “My keywords”;
Header.Controls.Add(keyw);
}
You can see in the example that I’ve defined the description tag as desc and the keyword tag as keyw, but any definition could be used here. Once this code is added, the content pages will automatically load the relevant <meta> tag information to the page when it loads.
So the above code would render into HTML in the content page as;
<meta name=”description” content=”My description for this page” />
<meta name=”keywords” content=”My keywords” />
Any static <meta> content that would duplicate it should be removed from the main template page. And now that the keywords and description tags are controlled through the code behind they can be easily changed on different content pages and easily altered without the need to make changes to the main HTML code.

Leave a Reply