(Recommended) Implementation of asp.net UrlRewrite Technology

Source: Internet
Author: User
First, go to the following address:

Bytes

Download the URLRewriter. dll of the ms and put it in the bin of your web program.

Note: The above URL downloads a complete URLrewrite technical example from Microsoft. The downloaded MSDNURLRewriting. msi file is installed on the local machine. After installation, there are three folders in the installation directory: ActionlessForm, RewriterTester, and URLRewriter. The URLRewriter folder is a complete URLRewrite project example. The BIN directory in this project contains two dll
ActionlessForm. dll and URLRewriter. dll are the dll class libraries generated by ActionlessForm and URLRewriter. They are the class library files used to implement URLRwrite Technology in the example project RewriterTester.

How to apply this technology to your project is actually very simple:

First, put the ActionlessForm. dll and URLRewriter. dll files in the bin directory of your project.
Then, modify your web. config file. The complete web. config file is as follows:

(Add two places in the common web. config file)
----------------------------------------------
1,

Add the following to the </configSections> label:
<Section name = "RewriterConfig" type = "URLRewriter. Config. RewriterConfigSerializerSectionHandler, URLRewriter"/>

2,

<HttpModules>
<Add type = "URLRewriter. ModuleRewriter, URLRewriter" name = "ModuleRewriter"/>
</HttpModules>

--------------------------------------------
<? Xml version = "1.0" encoding = "UTF-8"?>
<! --
Note: In addition to manually editing this file, you can also use
Web management tools to configure application settings. You can use
"Website"-> "Asp. Net configuration" option.
The complete list of settings and comments is displayed in
In machine. config. comments, this file is usually located in
\ Windows \ Microsoft. Net \ Framework \ v2.x \ Config
-->
<Configuration>

<ConfigSections>
<! -- The <configSections> element must contain a <section> tag for the <RewriterConfig> section element.
The type of the section handler is RewriterConfigSerializerSectionHandler, which is responsible
Deserializing the <RewriterConfig> section element into a RewriterConfig instance -->
<Section name = "RewriterConfig" type = "URLRewriter. Config. RewriterConfigSerializerSectionHandler, URLRewriter"
/>
</ConfigSections>

<RewriterConfig>
<Rules>
<! -- Rules for Blog Content Displayer -->
<RewriterRule>
<LookFor> ~ /(\ D {4})/(\ d {2})/(\ d {2}) \. aspx </LookFor>
<SendTo> ~ /ShowBlogContent. aspx? Year = $1 & amp; month = $2 & amp; day = $3 </SendTo>
</RewriterRule>
<RewriterRule>
<LookFor> ~ /(\ D {4})/(\ d {2})/Default \. aspx </LookFor>
<SendTo> <! [CDATA [~ /ShowBlogContent. aspx? Year = $1 & month = $2]> </SendTo>
</RewriterRule>
<RewriterRule>
<LookFor> ~ /(\ D {4})/Default \. aspx </LookFor>
<SendTo> ~ /ShowBlogContent. aspx? Year = $1 </SendTo>
</RewriterRule>

<! -- Rules for Product Lister -->
<RewriterRule>
<LookFor> ~ /Products/Default \. aspx </LookFor>
<SendTo> ~ /ListCategories. aspx </SendTo>
</RewriterRule>
<RewriterRule>
<LookFor> ~ /Products/Beverages \. aspx </LookFor>
<SendTo> ~ /ListProductsByCategory. aspx? CategoryID = 1 </SendTo>
</RewriterRule>
<RewriterRule>
<LookFor> ~ /Products/Condiments \. aspx </LookFor>
<SendTo> ~ /ListProductsByCategory. aspx? CategoryID = 2 </SendTo>
</RewriterRule>
<RewriterRule>
<LookFor> ~ /Products/Confections \. aspx </LookFor>
<SendTo> ~ /ListProductsByCategory. aspx? CategoryID = 3 </SendTo>
</RewriterRule>
<RewriterRule>
<LookFor> ~ /Products/Dairy \. aspx </LookFor>
<SendTo> ~ /ListProductsByCategory. aspx? CategoryID = 4 </SendTo>
</RewriterRule>
<RewriterRule>
<LookFor> ~ /Products/GrainsCereals \. aspx </LookFor>
<Sendto> ~ /Listproductsbycategory. aspx? Categoryid = 5 </sendto>
</Rewriterrule>
<Rewriterrule>
<Lookfor> ~ /Products/meatpoultry \. aspx </lookfor>
<Sendto> ~ /Listproductsbycategory. aspx? Categoryid = 6 </sendto>
</Rewriterrule>
<Rewriterrule>
<Lookfor> ~ /Products/produce \. aspx </lookfor>
<Sendto> ~ /Listproductsbycategory. aspx? Categoryid = 7 </sendto>
</Rewriterrule>
<Rewriterrule>
<Lookfor> ~ /Products/seafood \. aspx </lookfor>
<SendTo> ~ /ListProductsByCategory. aspx? CategoryID = 8 </SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>


<System. web>

<HttpModules>
<Add type = "URLRewriter. ModuleRewriter, URLRewriter" name = "ModuleRewriter"
/>
</HttpModules>

<! -- <HttpHandlers>
<Add verb = "*" path = "*. aspx" type = "URLRewriter. RewriterFactoryHandler, URLRewriter"
/>
</HttpHandlers> -->

<! --
Set compilation debug = "true" to insert the debugging symbol
Compiled pages. However, this
Performance is affected, so this value is only available during development.
Set to true.
-->
<Compilation debug = "false"
/>
<! --
In the <authentication> section, you can configure
Security Authentication mode,
To identify the user.
-->
<Authentication mode = "Windows"
/>
<! --
If an unprocessed error occurs during request execution,
You can configure the corresponding processing steps in the <mermerrors> section. Specifically,
This section allows developers to configure
Html error page to be displayed
To replace the error stack trace.

<CustomErrors mode = "RemoteOnly" defaultRedirect = "GenericErrorPage.htm">
<Error statusCode = "403" redirect = "NoAccess.htm"
/>
<Error statusCode = "404" redirect = "FileNotFound.htm"
/>
</CustomErrors>
-->
</System. web>
</Configuration>

Then, your Aspx program will convert the URL request address according to the regular expression in your web. config file to implement urlrewrit technology.
For example:

1. http: // localhost: 4789/guantesturlrewrit/2003/07/18. aspx
According to the regular expression in the web. config file, the URL address is rewritten to the following real address:
Http: // localhost: 4789/guantesturlrewrit/showblogcontent. aspx? Year = 2003 & month = 7 & day = 18

2. http: // localhost: 4789/guantesturlrewrit/2003/default. aspx
According to the regular expression in the web. config file, the URL address is rewritten to the following real address:
Http: // localhost: 4789/guantesturlrewrit/showblogcontent. aspx? Year = 2003

3. http: // localhost: 4789/guantesturlrewrit/products/confections. aspx
According to the regular expression in the web. config file, the URL address is rewritten to the following real address:
Http: // localhost: 4789/guantesturlrewrit/listproductsbycategory. aspx? Categoryid = 3

You can customize your own regular expressions to implement different URL rewriting rules.

If you want to rewrite aspx to the suffix of HTML, You need to modify your web. config file,
<Httphandlers>
<Add verb = "*" path = "*. aspx" type = "URLRewriter. RewriterFactoryHandler, URLRewriter"/>
<Add verb = "*" path = "*. html" type = "URLRewriter. RewriterFactoryHandler, URLRewriter"/>
</HttpHandlers>
In this case, the suffix is invalid in iis.html (I don't know how to name it ...)
Then, perform the following operations:
Right-click my computer --> Manage --> Expand 'services and Applications' --> internet Information Service --> Find the directory you shared --> right-click Properties --> click 'configuration' -->
Ing the following --> Find. aspx executable file path copy path --> paste path --> the extension is ". html "--> then remove the check object check box to check whether the object exists.

REFERENCE The ActionlessForm. dll file because the page contains Post data (such as Post text ). Then the URL after rewriting will change to: [url = http: // localhost/Test/2004/12] http: // localhost/Test/2004/12/News. aspx? ID = 12 [/url] the actual url exposes the original address, which is obviously not perfect,

Appendix: Why does the URL change to [url = http: // localhost/Test/2004/12] http: // localhost/Test/2004/12/News. aspx [/url]? ID = 12
In fact, it is very simple, because there is such a sentence in web. config:
<Sendto> ~ /Default. aspx? Id = $2 </sendto>
Before you replace the form, you can view the source code of the page. Your form action is (in the above example): default. aspx? Id = 12
That is, the final HTML generated on the. ASPX page is:
<Form ID = "form1" name = "form1" method = "Post" Action = "default. aspx? Id = 12 "> </form>

Solution:

First, import actionlessform. DLL into the bin directory of your project, and then reference this DLL in your vs.net project. In your original (that is, not rewritten). ASPX page

Step 1: add this sentence to the top of the Code:
<% @ Register tagprefix = "SKM" namespace = "actionlessform" assembly = "actionlessform" %>
Step 2:
<Form ID = "form1" method = "Post" runat = "server"> and </form>
Replace:
<SKM: Form ID = "form1" method = "Post" runat = "server"> and </SKM: Form>

In this way, when data is sent back to this page, it will not jump to the real URL.

Also, if you want to rewrite the format with a URL as a directory, you do not need to add default. aspx:
Http://localhost/test/2004/12
Create the corresponding directory and file default. aspx.
Example: http: // localhost/Test/2004/12
Create Directory 2004 and directory 12, and then create the Default. aspx file under directory 12. File content can be empty.

The reason is that if IIS does not find a directory or file, an error is reported.

In addition, you need to add the month 01, 02.03 ...... to the project ..... 12. Then, add default under the first directory. aspx, the content is only one sentence: <% @ Page %>. This prevents the user from failing to find the file after entering the Directory and pressing enter. An error occurs.

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.