How to Render MetaTags in ASP.NET Page

If you need to render dynamic meta-tags in ASP.NET page you can do this trick. This ASP.NET code will define HTML meta elements on an ASP.NET Page. However, the output is all mixed in one line. So how do you separate it?

Here is the sample code:
HtmlMeta metakeywrd = new HtmlMeta();
metakeywrd.Name = “keywords”;
metakeywrd.Content = “add keywords here”;

HtmlMeta metadesc = new HtmlMeta();
metadesc.Name = “description”;
metadesc.Content = “add meta description here”;

Page.Header.Controls.Add(metakeywrd);
Page.Header.Controls.Add(metadesc);

And here’s how to make it better:
Page.Header.Controls.Add(new LiteralControl(“\n”));
HtmlMeta metakeywrd = new HtmlMeta();
metakeywrd.Name = “keywords”;
metakeywrd.Content = “add keywords here”;

HtmlMeta metadesc = new HtmlMeta();
metadesc.Name = “description”;
metadesc.Content = “add meta description here”;

Page.Header.Controls.Add(metakeywrd);
Page.Header.Controls.Add(new LiteralControl(“\n”));
Page.Header.Controls.Add(metadesc);
Page.Header.Controls.Add(new LiteralControl(“\n”));

 

Read this blog to find out more: http://www.devcurry.com/2010/11/render-dynamic-metatags-in-aspnet-page.html

Leave a Reply

Your email address will not be published. Required fields are marked *