How to dynamically add and change meta tags in MasterPage using ASP.NET and CSharpIntroduction
This article demonstrates how to dynamically add and change meta tags when in MasterPage using ASP.NET and C#.
Now days lots of sites are based on CMS(Content Management System). So there is a need to dynamically add or change meta tags in page for better ranking in search engine.
Using Code
You can use the following simple code for this purpose:
Firstly provide an id to your Head Tag in Master Page
Now in the page where you want to change the Meta tags dynamically add the following code sample on the page load event :
//Find the Head Tag in Master Page
HtmlHead hd = (HtmlHead)Page.Master.FindControl("myHead");
//Adding keyword Meta Tag to Head Section
HtmlMeta htmMeta2 = new HtmlMeta();
htmMeta2.Attributes.Add("name", "keywords");
htmMeta2.Attributes.Add("content", "Put your keywords here...");
//Add Meta Tag to Head
hd.Controls.Add(htmMeta2);
//Adding description Meta Tag to Head Section
HtmlMeta htmMeta1 = new HtmlMeta();
htmMeta1.Attributes.Add("name","description");
htmMeta1.Attributes.Add("content", "Put your description here...");
//Add Meta Tag to Head
hd.Controls.Add(htmMeta1);
Now check the view sourse of your page you will find keyword and description meta tag there.
Happy Coding