0
Posted on 10:43 PM by prajeesh and filed under ,
The meta tags are essential part of the Search Engine Optimization, meta tags are used to provide keywords in web pages. When you are doing a CMS project, it is necessory to add keywords from control panel for each page, Now in ASP.NET 2.0 and above, you can add these meta tags programmatically. The HtmlMeta class provides programmatic access to the HTML <meta> element on the server. The HTML <meta>element is a container for data about the rendered page, but not page content itself.
The Name property of HtmlMeta provides the property name and the Content property is used to specify the property value. The Scheme property to specify additional information to user agents on how to interpret the metadata property and the HttpEquiv property in place of the Name property when the resulting metadata property will be retrieved using HTTP.
For Pages Not using Master Pages:
private void CreateMetaTags()
{
HtmlMeta hm = new HtmlMeta();
HtmlHead head = (HtmlHead)Page.Header;
hm.Name = "Keywords";
hm.Content = "keywords,asp.net, c# help";
head.Controls.Add(hm);
}

For Pages Using Master Pages:
The solution to this is to first provide an id to your Head Tag in Master Page :
< head runat="server" id="masterHead">
Now add the following code to page_load event of the requires page

//Find the Head Tag in Master Page
HtmlHead hdMaster = (HtmlHead)Page.Master.FindControl("masterHead");
HtmlMeta htmMeta = new HtmlMeta();
htmMeta.Attributes.Add("name","description");
htmMeta.Attributes.Add("content", "this is test content for meta description");
//Add Meta Tag to Head
hdMaster.Controls.Add(htmMeta);
//Adding keyword Meta Tag to Head Section
HtmlMeta hm2 = new HtmlMeta();
hm2.Attributes.Add("name", "keywords");
hm2.Attributes.Add("content", "asp.net,meta,keywords,dynamically,masterpage"); hdMaster.Controls.Add(hm2); Shout it kick it on DotNetKicks.com
0
Responses to ... Dynamically create meta tags using asp.net