What is the difference between URL Routing in ASP. NET and URL Rewriting in IIS? routingrewriting

Source: Internet
Author: User

What is the difference between URL Routing in ASP. NET and URL Rewriting in IIS? routingrewriting

Preface

The previous two posts mentioned the features of URL Routing, but many people have found that URL Routing is URl Rewriting, in fact, although both of them provide similar functions (improving friendly URLs to facilitate search and indexing), the principle and running cycle of the two are completely different, in this article, we will analyze the differences.

Example

Before analyzing the principle, let's take an example to test it (the iis url Rewrite module requires IIS7 support ).

1. Create the corresponding MVC program for the URL of Customer/1

First, create a common MVC3 program and a simple mermercontroller 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 put it in ViewBag for display in the view. The view code is as follows:

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

2. Create a corresponding web form program for the URL of Customer/1

Create a Customer. aspx file under the root directory of the same solution. The file code mainly accepts an ID parameter and displays it on the page. The 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> under asp.net web form: <label runat = "server" id = "lblId"> </label> </div> </form>

OK. First configure the route of Customre/1 in Global. asax. cs. The Code is as follows:

routes.MapRoute( "CustomerDetail", // Route name "Customer/{id}", // URL with parameters new { controller = "Customer", action = "Detail", id = UrlParameter.Optional });

Compile the access and run http: // localhost/customer/123. The result displayed on the page is:

Run the result in MVC: 123.

3. Install iis url Rewrite

Download and install iis url Rewrite at the following address, after the http://www.iis.net/download/URLRewrite is successfully installed, in the MVC project web. configure a URL rewrite rule in config. The Code is as follows (Note: system. webServer node ):

<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>

This rule indicates that a request similar to customer/1 is rewritten to customer. aspx? On id = 1, we compile the program and access the following http: // localhost/customer/123. The page displays the following results:
Under asp.net web form: 123

That is to say, Routing does not work at this time, because URL Rewrite forwards the Customer/123 address to Customer. aspx before Routing? Id = 123. The request is received by the aspx file. Let's take a look at the actual processing cycle between the two.
Cycle

1. URL Rewrite

URL Rewrite was first seen in apache web server. The popular url rewriting made a large number of php users ecstatic. Unfortunately, the implementation methods under asp.net were particularly complicated, therefore, many websites are basically useless. This problem has been completely solved since IIS.net launched the official iis url Rewrite module.

URL Rewrite is executed in the early stage of request-processing pipeline. Generally, after an address comes in, this module maps to another address on the same server according to the rules, the parameters and data accepted by the new address during processing are determined by a new address parameter, such as customer. aspx? Id = 123, while the url in the request is also a new address, instead of rewriting the previous address, but the user does not feel the change, because the browser always displays the old URL, let's take a look at his cycle chart.

The URL Rewrite module is a native module. The figure shows that its running cycle is between Pre-begin Rquest and Begin Request. After the module matches the rules according to the Request URL, the new URL is finally processed to enter the remaining cycle of IIS pipeline for processing, that is, the HttpHandler processing the request is determined by the new URL after rewriting.

2. URL Routing

The principle of URL Routing is to define the rule based on the existing URL and the HttpHandler corresponding to each rule. Its nature is irrelevant to the friendliness of the URL, only the corresponding HttpHandler is called according to the unified rules. Find the corresponding HttpHandler and return the result after processing. If no result is found, a resource error occurs.

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, this module queries the URL matching rules declared in all records in the routing table of the static set. If the current URL corresponds to a matched Handler, then the Handler is used to process the result and output it.
Differences between the two

The URL Rewrite module does not know which HttpHandler processes the request, in addition, the HttpHandler that processes the request does not know whether the URL it processes is the original URL or the address that has been rewritten.
In contrast to URL Rewrite, URL Routing specifies HttpHandler for URLs based on rules. It can be seen that Routing is an advanced handing of handler.
Iis url Rewrite can be used for ing of any web program, including but not limited to asp.net, php, asp, and static files. However, Routing can only process. net-based web programs.
Iis url Rewrite can be used in both IIS integration mode and classic mode, but Routing can only be used in integration mode by default, in Classic mode, you need to map the program to IIS using the extension or wildcard.. net has another component, aspnetfilter, which has already helped us ).
Iis url Rewrite can process Rewrite Rules Based on domain names, paths, Http headers, server variables, etc. However, Routing can only process the Rewrite rules based on the request URL path and HTTP-Method Header.
Iis url Rewrite can process HTTP redirects and customize Status Code and Abort requests, but Routing cannot.
The extension of iis url Rewrite can only extend the storage of custom rules of its Provider, but the extension of Routing is relatively powerful. MVC is one of them.

Summary

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

If your program is built on a platform other than asp.net, you can only use iis url Rewrite. Otherwise, we recommend the following rules:

If you are using asp.net to build a new asp.net web program, you can use Routing, because now Routing not only supports MVC, but also web form.
If your asp.net web form program is ready for use and you do not want to modify it, use URL Rewrite to help you improve search engine optimization.

Of course, you can also use the two together, but you must remember what the above explain: their execution cycles are different.

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

The difference between URL Routing in ASP. NET and URL Rewriting in IIS is that all the content is shared by Alibaba Cloud xiaobian. I hope to give you a reference and support for the help house.

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.