Asp. Net URL Routing and IIS URL rewriting the difference _ self-study process

Source: Internet
Author: User

Objective

There are 2 posts mentioned in the front about the characteristics of the URL routing, but found that a lot of people misunderstood URL routing is the URL rewriting, in fact, 2 although all provide similar functions (to improve the friendly URL to facilitate the search caused included), But the principle of the 2 and the operating cycle is completely different, this article we will analyze the specific difference.

Example

Before analyzing the principle, let's do an example to test (the IIS URL rewrite module requires IIS7 support).

1. Establish the corresponding MVC procedure for the CUSTOMER/1 URL

First build a common MVC3 program, build a simple customercontroller and a simple detail action, the code is as follows:

public class Customercontroller:controller
{public
 actionresult Detail (string id)
 {
  Viewbag.customerid = ID;
  return View ();
 }

We simply accept an ID and then put it in the viewbag to display it in view, and the view code is as follows:

@{
 Layout = null;
}
<! DOCTYPE html>
 
 

2. Create the corresponding Web Form program for the CUSTOMER/1 URL

In the root directory of the same solution, create a customer.aspx file, the file code is mainly to accept an ID parameter and then display on the page, code:

public partial class Customer:System.Web.UI.Page
{
 protected void Page_Load (object sender, EventArgs e)
 {
  This.lblId.InnerText = request.querystring["id"];
 }

Html:

<form id= "Form1" runat= "Server" >
 <div>
 asp.net web form is: <label runat= "Server" id= "Lblid" ></label>
 </div>
</form>

OK, we'll first configure CUSTOMRE/1 's route in Global.asax.cs, the code is as follows:

Routes. Maproute (
 "Customerdetail",//Route name
 "Customer/{id}",//URL with parameters
 New {controller = "custome R ", action =" Detail ", id = urlparameter.optional}
);

Compile access, run http://localhost/customer/123, and the results of the page display are:

Running under MVC results: 123.

3. Install IIS URL Rewrite

Download and install the IIS URL to the following address Rewrite,http://www.iis.net/download/urlrewrite successfully installed, Configure a URL rewrite rule in the web.config of the MVC project, the code below (note that the system.webserver node OH):

<system.webServer>
 <validation validateintegratedmodeconfiguration= "false"/>
 <modules Runallmanagedmodulesforallrequests= "true"/>
 <directorybrowse enabled= "true"/>
 <rewrite>
  <rules>
  <rule name= "rewrite customer" >
   <match url= "^customer/([0-9]+)"/>
   < Action type= "Rewrite" Url= "customer.aspx?id={r:1}"/>
  </rule>
  </rules>
 </rewrite >
</system.webServer>

The rule means that the CUSTOMER/1 similar request is rewritten to customer.aspx?id=1, we compile the program to access the following http://localhost/customer/123, the page displays the result as follows:
The ASP.net Web Form is: 123

That is to say routing did not work at this time, because the URL rewrite before routing customer/123 this address forward to customer.aspx?id=123, by the ASPX file to receive the request. Let's take a look at what cycle the 2 are dealing with.
Principle Cycle

1. URL Rewrite

About URL rewrite was first seen in the Apache Web server, when Rage URL rewrite made a large number of people using PHP ecstatic, Unfortunately, because the implementation of the asp.net under the special complexity, so many stations are basically not how to use, since the iis.net launched the formal IIS URL rewrite module, has completely solved the problem.

The URL rewrite is executed in the early stages of request-processing pipeline, when a general address comes in, the module maps to another new address on the same server based on the rules. The parameters and data that the new address accepts at the time of processing are the parameters of a new address, such as customer.aspx?id=123, and the URL in the request is a new address, not a rewrite of the previous address, but the user itself does not feel the change, Since the old URL is always displayed on the browser, let's look at his cycle chart.

The URL Rewrite module is a native module, from which you can see that his running period is between Pre-begin rquest and begin request, which, after a rule match based on the requested URL, The new URL is eventually processed for processing in the remainder of the IIS pipeline, i.e. the HttpHandler of the processing request is based on the new URL after the rewrite.

2. URL Routing

The principle of URL routing is to define the rules according to the existing URLs, define the corresponding HttpHandler of each rule, the nature of the URL is not related to the friendly, but according to the Uniform Rules to invoke the corresponding HttpHandler only, Find the corresponding HttpHandler, processing after the return of results, can not find the storm resource errors.

The

Routing is a managed code module that is registered in the Resolve cache cycle (Postresolverequestcache event) and then processed within the Maphandler cycle. In the Postresolverequestcache event, the module queries the URL matching rule declared in all records in the static set routing table, and if the current URL corresponds to a matching handler, then the handler is used to process the result and output.
The difference between the two

The URL rewrite is to modify the corresponding url,url before request processing rewrite the module itself does not know which HttpHandler handles the request. And the HttpHandler that handles the request does not know whether the URL it is dealing with is the original URL or the overridden address.
In contrast to the URL rewrite, the URL routing is specified HttpHandler according to the rule as the URL, which can be considered as an advanced mapping of handler.
IIS URL rewrite can be used for mapping processing of any Web program, including but not limited to asp.net,php,asp and static files, but routing can only handle Web programs based on. NET.
IIS URL rewrite can be used in both IIS Integrated mode and Classic mode, but by default routing can only be used in integrated mode. Classic mode requires that the program be mapped to IIS using either an extension or a wildcard (in fact. NET has another component Aspnetfilter has done for us).
IIS URL Rewrite can handle rewrite rules based on domain names, paths, Http header,server variables, and so on, but routing can only be processed according to the URL path and Http-method Header of the request.
The IIS URL rewrite can perform processing HTTP jumps, handle custom status code, and abort requests, but routing do not.
An extension of the IIS URL rewrite can only extend its own provider from the store that defines the rule, but the routing extension is relatively powerful, and MVC is one of them.

Summarize

There are so many differences between the two, so which one should we use?

If your program is built with a platform other than ASP.net, you can only use the IIS URL rewrite, otherwise, the rules we recommend are:

If you're building a new asp.net Web program with asp.net, use routing, because now routing not only supports MVC, it also supports Web form.
If your asp.net Web form is already available and you don't want to change it, use the URL Rewrite, which will help you improve your search engine optimization.

Of course, you can combine the two together, but before you use it, remember that the above diagram shows that their execution cycles are different.

Reference Address: http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/

The above asp.net URL Routing and IIS on the difference between the URL rewriting is a small series to share all the content, I hope to give you a reference, but also hope that we support cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.