For today's search engine optimized web sites having relevant content of the META Description HTML tag on a page is a must. It plays a significant role together with the page title in determining a page relevance ranking in regards to search keywords.
So it becomes necessary to generate a content of the META Description
dynamically based on a page content. ASP.NET already provides an easy access to the page TITLE
tag allowing to populate it with relevant text simply using a Page.Title
property.
Unfortunately it's not the same easy and obvious how to access the META Description
from the server-side code.
Luckily ASP.NET is flexible enough and the solution is easy. Simply add a META
tag to your page and turn it into a server tag by adding an id
and a runat="server"
attributes like the following:
<meta name="description" content="your description" runat="server" id="Description" />
Immediately you'll be able to access the content from the code behind by using a Description.Content
property.
The explanation is very simple, actually. When ASP.NET parses the mark-up it creates an HtmlMeta
server control with the name "Description" that has a Content
property of a string type. And, of course, you can employ the same technique if you want to manipulate with other META tags on the page. Just use the proper value of a "name" attribute.
Hey there, when working with master pages, this works fine as long as the code is in the master page code-behind. I'd like to use this easy way to access the meta description, but on a normal page. (I'm currently adding a new html meta object, which is a bit messy, frankly.)
ReplyDeleteTo mart:
ReplyDeleteAlternatively you can add an asp:ContentPlaceHolder server control inside the <head> tag on your master page and declaratively add a new <meta> tag inside the corresponding asp:Content control on your content page.