![]() |
Web Development »
ASP.NET »
General
Intermediate
License: The Code Project Open License (CPOL)
URL Mapping / URL Rewriting / Search Engine Friendly URLs / Virtual URLs (ASP.NET)By Manu AgrawalURL Mapping / URL Rewriting / Search Engine Friendly URLs / Virtual URLs with postbacks in ASP.NET C# (Without hardcoding in Web.Config) |
C#2.0.NET2.0, Win2K, WinXP, Win2003, Vista, ASP.NET, WebForms, VS.NET2003, VS2005, Dev
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
(ASP.NET, C#, Visual Studio 2005)
Hi folks; Recently, I built a custom Content Management Software for a website. The client wanted the page contents to be served from database by a page ShowContents.aspx?ContentID=XX.
The main requirements were as follows:
After a day's research, I came up with this really simple solution. I thought I should sum up the knowledge collected from various sources and make it public for everyone to benefit.
You can integrate following steps in to your existing ASP.NET project, or you can create a new one.
STEP 1
Context.RewritePath(Path.GetFileName(Request.RawUrl)); // needs System.IOSTEP 2
// STEP 1 (ShowContents.aspx) //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // This page displays contents from a database for a ContentID. This file // must contain the following line in it's Page_Load() event. Context.RewritePath(Path.GetFileName(Request.RawUrl)); //needs System.IO // STEP 2 (Global.asax) //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Following code goes in your Global.asax file, in the root directory of // the project. This file captures the incoming requests' URL, gets the // Content ID of the content related to the URL and redirects the user to // ShowContents.aspx?ContentID=XX. While the user still sees the requested // URL in the address bar. <%@ 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", ""); // You can retrieve the ID of the content from database that is // relevant to this requested URL (as per your business logic) int nId = 0; ////// 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); } } // That's all! :-)
Hope this helps you save time on researching various sources. You might like to download the zip file containing a sample VS 2005 project.
Feedback is welcome and appreciated.
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 21 Apr 2007 Editor: |
Copyright 2007 by Manu Agrawal Everything else Copyright © CodeProject, 1999-2010 Web09 | Advertise on the Code Project |