The new version of URL-Rewrite module for IIS 7.0 is released. With the release of. NET Framework 3.5 SP1, the ASP. NET routing component is further developed in. NET Framework 4.0 Beta. As an ASP. NET developer, we have many questions about the two components with similar functions, such as "what are their similarities and differences ?" "What environments are they applicable ?" And so on. This article aims to describe the similarities and differences between the two and provide developers with suggestions on which solution to use.
On the surface, these two technologies seem to provide very similar features: To provide websitesUser-friendly,Search engine friendly. However, there are essential differences in the principles of these two technologies. You need to have an in-depth understanding to make the right decisions when choosing an application. To help you understand these two technologies, let's start with their operating principles.
Translated fromIIS Official WebsiteThe terms used in China are partially adjusted.
URL rewriting)
URL rewriting is no longer a new technology and has been widely used on Apache servers around 10 years ago. Because it is recognized as a magic weapon for Web administrators and developers, many popular APACHE-based websites rely on URL rewriting to provide "elegant" URLs.
Simply put, the concept of URL rewriting is very simple: when a client requests a specific URL from the server, the URL rewriting module analyzes the requested URL and changes the request, point to another URL on the same web server. It is very easy to insert the URL rewriting module (for example, httpmodule) in the Web Request cycle. Therefore, it can modify the requested URL before the server processes the request. Server ProcessingProgramA response is generated to the client browser based on the rewritten URL. Because the rewrite process occurs on the server, the client browser cannot see the rewritten URL. in the client's view, the response it receives comes from the original request URL.
In the architecture of IIS 7.0, this process can be illustrated as follows:
The URL-Rewrite module provided by Microsoft is a native-code module that inserts Web Request ProcessingPre-begin requestOrBegin requestAnd then rewrite the URL by configuring a series of rewrite rules. Each rewrite rule performs regular analysis on the URL to determine whether the currently requested URL meets the conditions defined by the rewrite rule, rewrite the original URL into a new URL according to the rule. After all the rewrite rules are passed, the URL rewriting module generates a final URL path and passes it to the remaining request processing process. That is to say, the request handler in the IIS pipeline processes the URL written by the URL rewriting module.
ASP. NET routing (ASP. NET Routing)
Essentially, ASP. NET routing is a request scheduling mechanism that allows developers to associate URLs that comply with specified rules with a handler that can process the request. This association is completed by registering the "Routes" that defines the URL ing relationship. When a request arrives at the server, the ASP. NET routing module searches for the registered routes table. If a route is matched, the corresponding route handler is called to process the request.
In the architecture of IIS 7.0, this process can be illustrated as follows:
ASP. NET routes are hostedCodeIs embedded into the IIS request processing pipeline as a plug-in.Resolve CacheSTEP (PostresolverequestcacheEvents) andMap HandlerSTEP (Postmaprequesthandler. ASP. NET routes are configured to take effect for global requests of Web applications.
InPostresolverequestcacheDuring the event, the routing module searches for a route table (a set of Route objects) to find the route that matches the requested URL. If a matched route is found, the ASP. net routing module obtains a handler that processes the route and saves the reference of the handler to the current httpcontext. Of course, this handler can be implemented.System. Web. ihttphandlerAny class of the interface (such as the page class ). If the matched route cannot be found, Asp. the net routing module does nothing and allows the request to complete the remaining processing process (usually mapped to a hard disk file such as XX. aspx ).
InPostmaprequesthandlerDuring the event, the routing module checks whether the current httpcontext contains a handler information. If so, the ASP. NET routing moduleHandlerSet the property to the handler. In this wayExecute HandlerIn the step, execute the handler selected by the routing module. If no, the ASP. NET routing module does nothing. Let IIS select a handler to process this URL request.
Differences between URL rewriting and ASP. NET Routing
Based on the above description, we can see that the differences between URL rewriting and ASP. NET routing are as follows:
- URL rewriting is used to process the URL before the Web server processes the request. The URL rewriting module does not know what handler will be used to process the rewritten URL. Similarly, the final handler does not know whether the URL has been overwritten.
- ASP. net routing is used to assign a corresponding handler (handler) for a request based on the URL. Unlike URL rewriting, the routing component knows the relevant handler, and select handler to process the requested URL. In view of this, we can also consider ASP. net routing as an advanced Handler-mapping mechanism.
In addition to conceptual differences, URL rewriting and ASP. NET routes also have the following functional differences:
- The iis url rewriting module can be used for any type of Web applications, including ASP. NET, PHP, ASP, and even static files. ASP. NET routes can only process. NET Framework-based Web applications.
- The iis url rewriting module runs the same in the integration and classic IIS pipeline mode application pools. ASP. NET routes tend to be in the Application Integration pipeline mode. ASP. NET routes can also be run in Classic mode, but the URL in the program must contain the file extension, or the program must configure a "*" handler ing in IIS.
- The URL rewriting module can determine whether to rewrite the domain name, HTTP header information, and server variables. By default, ASP. NET routes can only be routed through the URL path and HTTP-method information in the HTTP header.
- In addition to URL rewriting, the iis url rewriting module can also perform HTTP redirection, set custom IIS status codes, and even terminate requests. ASP. NET routes cannot do this.
- The iis url rewriting module cannot be expanded in the current version, while ASP. NET routes are completely scalable and customizable.
How should we choose?
Once we know the similarities and differences, how should we select a suitable URL optimization solution for the application? We will discuss it in the next section.