How to Programmatically Define multiple META Tags for SEO to Asp.Net page?

 Copyright © 2007-2012 www.AspDotNetFaq.com

The easiest way to add META TAGS to the <head> element of your web page is to add it
declaratively in HTML of your ASP.NET page like this:

     <META name="Description" content="Asp.Net FAQs Home Page."

That was easy alright.

But what if we need to add META TAGS dynamically,
for example if we want to add different Keywords meta tags for each page
depending on the actual content of the page?

There is an easy way to accomplish this thanks to the ASP.NET.

All we need to do is to add the following code to our Page_Init method:
 

 

    protected void Page_Init(object sender, EventArgs e)

    {

        HtmlMeta meta1 = new HtmlMeta();

        meta1.Name = "Description";

        meta1.Content = "Page description";

        this.Page.Header.Controls.Add(meta1);

 

        HtmlMeta meta2 = new HtmlMeta();

        meta2.Name = "Keywords";

        meta2.Content = "Asp.Net .NET";

        this.Page.Header.Controls.Add(meta2);

    }


note:
This also works with MasterPages. Just place this code in your content page.
 

 






 Copyright © 2007-2012 www.AspDotNetFaq.com