Source code location: ./page_titles/pagetitlemodule.cs
using System.Web; using System; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Text; public class PageTitleModule : IHttpModule { // ------------------------------------------------------------------------ public void Init (HttpApplication context) { context.PreRequestHandlerExecute += new EventHandler (context_PreRequestHandlerExecute); } // ------------------------------------------------------------------------ void context_PreRequestHandlerExecute (object sender, EventArgs e) { Page page = HttpContext.Current.CurrentHandler as Page; if (page != null) { page.Load += new EventHandler (page_Load); } } // ------------------------------------------------------------------------ void page_Load (object sender, EventArgs e) { SiteMapNode node = SiteMap.CurrentNode; if (node != null) { Page page = HttpContext.Current.CurrentHandler as Page; if (page != null) { StringBuilder titleBuilder = new StringBuilder (); if (!string.IsNullOrEmpty (node.Title)) titleBuilder.Append (node.Title); /* Note: instead of stuffing the description into the page title, * you can add a <description> meta tag instead. */ if (!string.IsNullOrEmpty (node.Description)) { if (titleBuilder.Length > 0) titleBuilder.Append (" | "); titleBuilder.Append (node.Description); } page.Title = titleBuilder.ToString (); /* This is a little extra: if there's a "keyword" attribute for this node, \ * add it as a meta tag. */ string keywords = node["keywords"]; if (!string.IsNullOrEmpty (keywords)) { HtmlMeta keywordsMeta = new HtmlMeta (); keywordsMeta.Name = "keywords"; keywordsMeta.Content = keywords; page.Header.Controls.Add (keywordsMeta); } } } } // ------------------------------------------------------------------------ public void Dispose () { } }
Copyright © 2004-2006, Milan Negovan. All rights reserved. | License
Back to AspNetResources.com