Creating Vanity URLs


One interesting feature of the ASP.NET job site is its use of vanity URLs. Each user who registers at the job site is automatically given a personalized home page. For example, if you register with the username marksmith , you can access your home page by going to /aspnetjobs/marksmith.aspx (see Figure 30.4).

Figure 30.4. A vanity URL.

graphics/30fig04.jpg

The vanity URLs are implemented with two files: the Global.asax and UserPage.aspx files. The Global.asax file checks the path of each request made to the job site. When appropriate, the Global.asax file rewrites the path of the request to the UserPage.aspx page. The complete code for the Global.asax file is contained in Listing 30.7.

Listing 30.7 Global.asax
 <%@ Import Namespace="System.IO" %> <Script Runat="Server"> Sub Application_BeginRequest   Dim strPageOwner As String   Dim strCurrentPath As String   Dim strCustomPath As String   strCurrentPath = Request.Path.ToLower()   If strCurrentPath.IndexOf( "/site/" ) = -1 _   AND strCurrentPath.IndexOf( "userpage.aspx" ) = -1 _   AND strCurrentPath.IndexOf( "default.aspx" ) = -1 Then     strCustomPath = _       String.Format( _         "/aspnetjobs/userpage.aspx?pageowner={0}", _         Path.GetFileNameWithoutExtension( Request.Path ) )     Context.RewritePath( strCustomPath )   End If End Sub </Script> 

The C# version of this code can be found on the CD-ROM.

The Global.asax file in Listing 30.7 handles the BeginRequest event that is raised whenever someone requests a page from the job site. When appropriate, the RewritePath method is used to redirect a page request to the UserPage.aspx page.

First, the Application_BeginRequest subroutine checks whether the request is for the Default.aspx page, the Userpage.aspx page, or a page below the /site/ subdirectory. These requests are ignored. You only want to redirect a user when the user makes a request with a vanity URL. You must be careful not to redirect users when they are requesting actual pages at the job site.

NOTE

You also can implement vanity URLs by creating a custom HTTP module. An HTTP module is a class that is executed whenever a request is made for an ASP.NET page. To learn more about creating HTTP modules, see Chapter 15, "Creating ASP.NET Applications."


If the user is not requesting an actual page at the Web site, the RewritePath method is used to redirect the user to the UserPage.aspx page. For example, if the user requests

 
 /aspnetjobs/marksmith.aspx 

the Global.asax file automatically rewrites the request like this:

 
 /aspnetjobs/userpage.aspx?pageowner=marksmith 

The value of the pageowner query string variable is captured within the UserPage.aspx page. The pageowner variable is used within the UserPage.aspx page to look up and display the information for the appropriate user from the database.

When using the Global.asax file in Listing 30.7, you must be cautious about the physical layout of the files in your Web site. All the actual files for the job site are located beneath the /site/ subdirectory. If you add a new page to the root directory, the page could never be accessed because any requests for the page would be interpreted as a request for a vanity URL. If you need to add new files to the ASP.NET job site, add the new files beneath the /site/ subdirectory.



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net