IMG-LOGO

How to add meta tags dynamically in ASP.Net C#?

andy - 01 Aug, 2013 6446 Views 0 Comment

Sometimes when you integrate other module or software in your current site, you want to add the meta tags dynamically in order to get a good SEO result. In this tutorial, you will learn how you can add meta tags dynamically in ASP.Net C#. We will add two meta tags description and keywords into a page.

//make sure the following namespace is imported
using System.Web.UI.HtmlControls;

protected void Page_Load(object sender, EventArgs e){
	//we get the header object
	HtmlHead objHeader = (HtmlHead)Page.Header;

	 //we add meta description
	HtmlMeta objMetaDescription = new HtmlMeta();
	objMetaDescription.Name = "DESCRIPTION";
	objMetaDescription.Content = "This will be the meta description content";

	//alternatively you can an ID attribute as well
	objMetaDescription.Attributes.Add("id", "MetaDescription");

	//This will add it to the header object
	objHeader.Controls.Add(objMetaDescription);

	//we add meta keywords
	HtmlMeta objMetaKeywords = new HtmlMeta();
	objMetaKeywords.Name = "KEYWORDS";
	objMetaKeywords.Content = "This will be the meta keywords content";

	//alternatively you can an ID attribute as well
	objMetaKeywords.Attributes.Add("id", "MetaKeywords");

	 //This will add it to the header object
	objHeader.Controls.Add(objMetaKeywords);
}

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.

Related Articles

How to remove html tags from string in c#?

Sometimes you need to remove HTML tags from string to ensure there are no dangerous or malicious scripts especially when you want to store the string or data text into the database.