Setting Meta Tags in ASP.NET with C# 3.0 Extensions

kick it on DotNetKicks.com

Here’s an extension on the System.Web.UI.Page class (the class for all “.aspx” files). The idea is to have keys in the Web.config appSettings that are the web site’s meta description and keyword tags, and they are set by calling this.SetMetaTags() on Page_Load() of your Page class.

using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;

public partial class _Default: Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		// Set meta header tags; SetMetaTags throws
		// ArgumentNullException
		this.SetMetaTags(
			Website.AppSettings["DefaultMetaDescription"],
			Website.AppSettings["DefaultMetaKeywords"]);

	}
}

// This is actually in App_Code/Extensions, and PageExtensions is
// not within a namespace

public static class PageExtensions
{
	/// <summary>
	/// Sets the HTML meta description and keyword tags of a
	/// <c>Page</c>.
	/// </summary>
	/// <exception cref="ArgumentNullException">
	/// If description or keywords are null </exception>
	/// <exception cref="InvalidOperationException">
	/// If page.Header is null</exception>
	public static void SetMetaTags(this Page page, string description,
		string keywords)
	{
		if (description == null)
			throw new ArgumentNullException("description");

		if (keywords == null)
			throw new ArgumentNullException("keywords");

		HtmlHead header;

		if ((header = page.Header) == null)
				throw new InvalidOperationException
				("The page's markup must have runat=server in its <head>");

		ControlCollection headerControls = header.Controls;

		// New C# 3.0 feature: object initializers
		headerControls.Add(new HtmlMeta
		{
				Content = description,
				HttpEquiv = "description"
		});
		headerControls.Add(new HtmlMeta
		{
				Content = keywords,
				HttpEquiv = "keywords"
		});
	}
}
Advertisements

4 Comments »

  1. Laurence said

    Where did this argument syntax (this Page page,…) come from?

    If it’s the same as (Page page,…), doesn’t it seem a little long-winded?

  2. Chris O. said

    Hey Laurence. The syntax you mention is a feature of C# 3, referred to as “extensions.” If you’ve every programmed JavaScript and used prototypes, extensions accomplish a very similar thing. Check out the new language features in 3.0–they’re pretty cool!

  3. Daniel said

    Doesn’t work with MasterPages ?

  4. جهت دیدن دوره های آموزشی کلید سازی و مهر سازی به وب سایت زیر مراجعه فرمائید.
    http://key.bitasoft.ir/
    mehrdad.kelid@gmail.com
    اصفهان – خیابان امام خمینی – بعد از سه راه ملک شهر – روبروی اداره برق
    تلفن همراه : 09131055395 – نجف زاده

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: