Method 3 for URL rewriting in ASP. NET: Use httpmodule in iis7 to rewrite a URL without an extension

Source: Internet
Author: User

The above httpmodule method works when the URL you want to override contains the. aspx extension or another extension that is set to ASP. NET. If you do this, you don't need any specific server configuration. You just need to copy your application to the remote server, and it will work normally.

But sometimes, the URL you want to rewrite either has an extension (for example,. jpg,. GIF, or. htm) that is not processed by ASP. NET, or has no extension at all. For example, we may want to display these URLs as public product directory webpages (note that they do not have the. aspx extension ):

 

Http://www.store.com/products/Books
Http://www.store.com/products/DVDs
Http://www.store.com/products/CDs

In iis5 and IIS6, it is not easy to process the above URL using ASP. NET. IIS 5/6 makes it difficult to rewrite these types of URLs in ISAPI extensions (ASP. NET is such an extension. What you need to do is to use the ISAPI filter to rewrite the IIS request pipeline (request pipeline) earlier. I will demonstrate how to implement this rewrite in iis5/6 in the fourth method below.

But the good news is that IIS 7.0 makes it easy to handle such situations. You can now execute an httpmodule anywhere in the IIS request pipeline, which means you can use the urlrewriter module above to process and override URLs without extensions (or even URLs. ASP ,. PHP, or. JSP extension URL ). The following shows how to configure iis7:

 

<? XML version = "1.0" encoding = "UTF-8"?>

<Configuration>

<Configsections>
<Section name = "rewriter"
Requirepermission = "false"
Type = "intelligencia. urlrewriter. configuration. rewriterconfigurationsectionhandler, intelligencia. urlrewriter"/>
</Configsections>

<System. Web>

<Httpmodules>
<Add name = "urlrewriter" type = "intelligencia. urlrewriter. rewriterhttpmodule, intelligencia. urlrewriter"/>
</Httpmodules>

</System. Web>

<System. webserver>

<Modules runallmanagedmodulesforallrequests = "true">
<Add name = "urlrewriter" type = "intelligencia. urlrewriter. rewriterhttpmodule"/>
</Modules>

<Validation validateintegratedmodeconfiguration = "false"/>

</System. webserver>

<Rewriter>
<Rewrite url = "~ /Products/(. +) "to = "~ /Products. aspx? Category = $1 "/>
</Rewriter>

</Configuration>

 

Note the runallmanagedmodulesforallrequests attribute set to true in <system. webserver> <modules>. This attribute ensures that the urlrewriter. Net module from intelligencia (written before the official release of iis7) is called and has the opportunity to rewrite all URL requests (including folders) to the server ). The preceding web. config file is very cool:

1) It works on any iis7 machine. You do not need the Administrator to enable anything on the remote host. It can also be set to medium trust) in the shared host environment.

2) Because I have configured urlrewriter in the

IIS 7.0 will be released later this year as part of the Windows Longhorn server. It will support the go-live license with the release of beta3 in a few weeks. Thanks to all the new host features added to iis7, we expect that the host provider will be very quick to actively provide iis7 accounts, this means that you should soon be able to use the above URL rewriting support without extension. We will release a URL rewriting module supported by Microsoft in iis7 RTM. This template is free of charge and can be used on iis7, this module also provides good support for advanced URL rewriting scenarios of all content on your Web server.

Sample download: A sample application that uses iis7 and urlrewriter. Net modules to demonstrate URL rewriting without extension can be downloaded here.

 

Source: http://hi.baidu.com/kent_edwin/blog/item/841aa20e23524be4aa64570d.html

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.