Dynamic META Tags - High-Flying C# Coding Tips


Sponsored Links

73058_New Scooba® 230 Floor Washing Robot + Free Shipping!

 

Dynamic META Tags

Here we'll look at a way of creating Dynamic META Tags. (.NET 2.0)

Usually, a HTML page contains META tags in the HEAD section. META Tags are used to give some kind of description about the page and are used by search engines. The values are usually set in the static HTML, fixed in code by the time they are presented to the user.

Here are the META tags in the HEAD section of this actual page.


<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Description" content="High-Flying C# Coding Tips - Dynamic META Tags" />
<meta name="Subject" content="High-Flying C# Coding Tips - Dynamic META Tags" />
<meta name="Title" content="High-Flying C# Coding Tips - Dynamic META Tags" />
<meta name=....


But you may want to be able to fill in these meta tags dynamically, that is use C# to control what goes into the META tags in the HTML which the user will see.

In this example, we create HtmlMeta objects, assign a name (the meta name), assign some content (meta content) and finally add the HtmlMeta object we created to the Header Controls collection. You'd want to run this code in the Page_Load event so the dynamic meta tags are added to the HTML page sent to the user when they view the page.



// feed the meta data
HtmlMeta _metaDescription = new HtmlMeta();
_metaDescription.Name = "Description";
_metaDescription.Content = "High-Flying C# Coding Tips - Dynamic META Tags";
this.Header.Controls.Add(_metaDescription);

HtmlMeta _metaSubject = new HtmlMeta();
_metaSubject.Name = "Subject";
_metaSubject.Content = "High-Flying C# Coding Tips - Dynamic META Tags";
this.Header.Controls.Add(_metaSubject);

HtmlMeta _metaTitle = new HtmlMeta();
_metaTitle.Name = "Title";
_metaTitle.Content = "High-Flying C# Coding Tips - Dynamic META Tags";
this.Header.Controls.Add(_metaTitle);


`