Displaying multiple static paths by single page
Posted by Tihomir Ivanov on 05 July 2009 18:04
Rating: 0.00
Do you have many pages like these:
http://www.yoursite.com/Default.aspx?page=page1
http://www.yoursite.com/Default.aspx?page=page2
...
http://www.yoursite.com/Default.aspx?page=pageN
For all these dynamic paths, you use only Default.aspx page to display them,
it would be better (especially for SEO) if you can have pages like:
http://www.yoursite.com/page1.aspx or http://www.yoursite.com/page2.aspx
You can do it very easy that using asp.net, just follow the steps below:
1. Add Global.asax file in your asp.net app
2. Handle Application_BeginRequest and add the code below:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
HttpContext incoming = HttpContext.Current;
string oldpath = incoming.Request.Path.ToLower();
string pageid; // page id requested
// Regular expressions to get the page id from the pageN.aspx
Regex regex = new Regex(@"page(\d+).aspx", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
MatchCollection matches = regex.Matches(oldpath);
if (matches.Count > 0)
{
// Extract the page id and send it to Process.aspx
pageid = matches[0].Groups[1].ToString();
incoming.RewritePath("Default.aspx?page=" + pageid);
}
else
{
// Display path if it doesn’t containt pageX.aspx
incoming.RewritePath(oldpath);
}
}
That's really everything you need to do. In your Default.aspx code-behind class you can get the page param by the Request.QueryString method:
protected void Page_Load(object sender, EventArgs e)
{
...
string pageId = Request.QueryString["page"];
...
}
That's all :)
Comments:
No comments yet.