Summary: Describes how to perform dynamic URL rewriting using Microsoft asp.net. URL rewriting is the process of intercepting incoming WEB requests and automatically redirecting requests to other URLs. Discusses the various techniques for implementing URL rewriting, and describes some of the realities of implementing URL rewriting.
Introduction
Let's take a moment to look at some of the URLs on the site. Do you find something similar to http://yoursite.com/info/dispEmployeeInfo.aspx? Empid=459-099&type=summary's URL? Alternatively, you may be able to move a series of web pages from one directory or Web site to another, resulting in a broken link for visitors who have used the old URL as a bookmark. In this article, we will learn how to pass the http://yoursite.com/info/dispEmployeeInfo.aspx? Empid=459-099&type=summary with URLs similar to Http://yoursite.com/people/sales/chuck.smith, using URL rewriting to add those lengthy URLs Abbreviations are meaningful and easy to remember URLs. We'll also learn how to use URL rewriting to create a smart 404 error.
URL rewriting is the process of intercepting incoming WEB requests and automatically redirecting requests to other resources. When a URL rewrite is performed, the requested URL is typically checked and the request is redirected to a different URL based on the value of the URL. For example, you might want to use URL overrides to check whether Web requests point to files in the/people/directory when you move all the pages in the/people/directory to the/info/employees/directory while you are reorganizing the site. If the request points to a file in the/people/directory, you may want to automatically redirect the request to the same file in the/info/employees/directory.
Using traditional ASP, the only way to apply URL rewriting is to write an ISAPI filter, or to purchase a Third-party product that provides URL rewriting capabilities. However, with microsoft®asp.net, there are many ways you can easily create your own URL rewrite software. This article discusses the various techniques that are available for ASP.net developers to implement URL rewriting, and then discusses some of the actual usage of URL rewriting. Before delving into the technical details of URL rewriting, let's look at some of the day-to-day scenarios that you can rewrite with URLs.
Common usage of URL overrides
When you create a data-driven asp.net web site, you typically produce a single web page that displays a subset of database data based on query string parameters. For example, when designing an E-commerce site, one of your tasks is to allow users to browse for products for sale. To do this, you can create a page named Displaycategory.aspx that displays the product for a given category. You can specify the product for this category to view by querying the string parameter. That is, if the user wants to browse for the widget product for sale, and all widget products have a CategoryID of 5, the user can access the following URL: http://yousite.com/displayCategory.aspx? Categoryid=5.
There are two deficiencies in creating a Web site with this type of URL: First, the URL http://yousite.com/displayCategory.aspx?CategoryID=5 is messy from the end user's point of view. Usability expert Jakob Neilsen recommends following criteria to select a URL:
• |
Short. |
• |
Easy to type. |
• |
You can see the structure of the site. |
• |
Can be truncated to allow the user to browse the site by removing part of the URL. |
I also want to add a standard, that is, the URL should be easy to remember. URL http://yousite.com/displayCategory.aspx?CategoryID=5 does not conform to any of the Neilsen standards, nor is it easy to remember. Requiring the user to type a query string value makes typing of the URL difficult, and only experienced Web developers who understand the purpose of the query string parameter and its name/value pair structure can "abridge" the URL.
A better approach is to allow the use of realistic and easily memorized URLs, such as Http://yoursite.com/products/Widgets. With a glance at the URL, you can infer what will be displayed-information about the Widget. This URL is also easy to remember and share. I can tell my colleague, "Please check yoursite.com/products/widgets," and she may open the page without asking me again what the URL is. (Try it, you can just say "Amazon.com page"!) This URL will also be displayed and should be "abridged". That is, if the user deletes the end of the URL, type http://yoursite.com/products, they should see a list of all the products, or at least see a list of all the categories they can view.
Note: To get the best example of an "abridged" URL, consider using URLs generated by many blog engines. To view a post on January 28, 2004, users can access URLs such as HTTP://SOMEBLOG.COM/2004/01/28. If the URL is truncated to http://someblog.com/2004/01, the user will see all posts for January 2004. Further abridged this URL to http://someblog.com/2004 will display all posts for 2004 years.
In addition to simplifying URLs, url rewriting is often used to process Web site reorganization, lest it cause a large number of links to break or expire bookmarks.
What happens when a request arrives at IIS
Before you formally study how URLs implement overrides, it is important to understand first how microsoft®internet information Services (IIS) handles incoming requests. When the request arrives at the IIS Web Server, IIS checks the file's extension to determine how the request is processed. IIS can process requests, such as HTML pages, images, and other static content, or route requests to an ISAPI extension. (An ISAPI extension is an unmanaged compilation class that handles incoming WEB requests.) The task is to generate the content of the requested resource. )