Dynamically create Meta Description using Asp.Net

← PrevNext →

Last update: 24th April 2024

HTML meta description allows us to describe web pages with short and sometimes elaborated details. Descriptions written inside the <meta> tag, should always be specific and are often limited to very few characters. Using Asp.Net, we can easily create "meta" descriptions dynamically, through code behind procedures, describing the contents of the page.

Note: Not just limited characters (as defined by Google and other search engines) but also similar or duplicate descriptions must not be repeated in other pages. Though meta description do not directly effects the web ranking of a site, but lengthy and duplicate meta descriptions does not go well in the eyes of the search engine.

Search engines do not need lengthy descriptions to understand the contents of a web page. All it needs is a more specific and a short description (about 150 to 160 characters) describing the contents of the page.

Meta descriptions can be updated and created dynamically using Asp.Net from code behind.

HTML meta description

A meta description is written inside a meta tag, within the <head></head> tag in a web page. This is where you describe the page with a small summary of the contents of the page.

<head>
    <meta name="description" content="Article on HTML meta description" />
</head>

The content attribute has the summary and in the above case, its written (or assigned) while page declaration.

Now imagine a scenario where you have "dynamic contents", and the descriptions must be created dynamically too. The source of these contents can be anything, extracted from a database or a file. All we want is to display it to our web pages, dynamically.

Add meta description dynamically

Code Behind (C#)
protected void form1_Load(object sender, System.EventArgs e)
{
    HtmlMeta metaDescription = new HtmlMeta();

    metaDescription.Name = "description";
    metaDescription.Content = "Article on HTML meta description";
    Header.Controls.Add(metaDescription);
}
VB
Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load

    Dim metaDescription As New HtmlMeta

    metaDescription.Name = "description"
    metaDescription.Content = "Article on HTML meta description"
    Header.Controls.Add(metaDescription)
End Sub

In the above codes, the HtmlMeta class will allow programmers to access the HTML <meta /> tag programmatically.

The property, "Name" has the value description

The property "Content" has the summary to describe the page.

Finally add the "Name" and "Content" properties to the page header using the Header property of the "HtmlHeader" class.

Important

As I have mentioned earlier in this article that "meta" tags are assigned within the <head> tags. It is important to remember that the <head> tag should have the attribute runat="server" defined in the page declaration itself. Else, it will throw an error saying, "Object reference not set to an instance of an object."

<head runat="server">

</head>
Conclusion

Dynamically created "meta" descriptions are useful when the contents of the web pages are extracted from another source and if the web page has paginations. Pagination typically refers to a process of dividing contents into many web pages, using "next", "previous" and "number" links.

👉 Sometimes we use a single page to display different contents. Therefore, to avoid duplicate meta descriptions on every page, create specific descriptions dynamically, explaining the contents of various pages.

← PreviousNext →