Sunday 8 April 2007

URL Mapping-Rewriting, ASP.NET C-Sharp

Problem
Hi folks. Recently, I did custom content management software for a website.
The client wanted
  • The content to be editable and to be served from database.
  • Search engine friendly URLs (e.g. They would like “/finance/loan.aspx” instead of “ShowContents.aspx?ID=23”).
  • Postback should still work on these virtual URLs.
  • References to the images, css files, themes should stay intact.
Solution
After about a day’s research, I came up with following simple solution.
You can download the code from my article URLMapping

STEP 1
  • Create “ShowContents.aspx", that serves content based on the ContentID passed in the QueryString.
  • To enable postbacks to the raw URL in the Page_Load() event of ShowContents.aspx, insert following line
    Context.RewritePath(Path.GetFileName(Request.RawUrl));
STEP 2
  • Capture the URL requested by the user in the Application_BeginRequest() event of Global.asax.
  • Find the ID of the content from database that is relevant to this URL
  • ReWritePath to ShowContents.aspx?ContentID=XX. The user will see requested URL in the address bar with the desired content.
  • Any files that physically exist on the server will still get served as usual.

Code

File: Global.asax (Create one if does not already exist)


<%@ Import Namespace="System.IO" %>

protected void Application_BeginRequest(object sender, EventArgs e)

{

// If the requested file exists

if (File.Exists(Request.PhysicalPath))

{

// Do nothing here, just serve the file

}

// If the file does not exist then

else if (!File.Exists(Request.PhysicalPath))

{

// Get the URL requested by the user

string sRequestedURL = Request.Path.Replace(".aspx", "");

// To get ID of the content from database

int nId = 0;

// You can retrieve the ID of the content from database that is relevant to this URL

////// nId = GetContentIDByPath(sRequestedURL); \\\\\

// The ShowContents.aspx page should show contents relevant to the ID that is passed here

string sTargetURL = "~/ShowContents.aspx?ContentID=" + nId.ToString();

// Owing to RewritePath, the user will see requested URL in the address bar

// The second argument should be false, to keep your references to images, css files

Context.RewritePath(sTargetURL, false);

// ###### I M P O R T A N T ######

// To enable postback in ShowContents.aspx page you should have following

// line in it's Page_Load() event. You will need to import System.IO.

// Context.RewritePath(Path.GetFileName(Request.RawUrl), false);

}

}