Why is Shenma URL rewriting?

Source: Internet
Author: User

Generally, when processing browser requests, the server uses the URL path sent by the browser to process the requests. Traditionally, people prefer to use "/" to split the URL information, for example: Signature? UN = yinhuichao.

Therefore, in order to cater to users' preferences and make it easier for users to remember URL addresses, we recommend using URL rewriting technology during development.

Let's take the previous two URLs as an example. When a user enters the first URL address request on the browser side, our server needs to analyze the user URL and process the user's URL, finally, it becomes the second URL form, so that you can access the page you want to access and pass the parameters to the target page.

I understand what URL rewriting is. How can I implement URL rewriting?

The following is a simple example to introduce the specific usage of URL rewriting:

1. First add a project class library named "urlrewritermodule.

2. Add "system. Web" reference to this class library.

3. Add a class named "urlrewriter" and implement the ihttpmodule interface.

4. register the method in the beginrequest event of the httpapplication pipeline. In this method, analyze the user URL and process the URL, finally, the processed URL is returned (that is, the request is sent to a specific page path ).

5. The Code is as follows:

Class urlrewrite: ihttpmodule {public void Init (httpapplication context) {// The beginrequest registration method context to the first event in the request pipeline. beginrequest + = beginrequestchuli;} void beginrequestchuli (Object sender, eventargs e) {// complete URL analysis and URL rewriting // 1. obtain the httpapplication object httpapplication application = sender as httpapplication for executing the current page request pipeline; // 2. obtain the context object httpcontext context = application. context; // 3. obtain the URL of the current request (excluding the domain name and request parameters) ST Ring urlwithoutpara = context. request. URL. localpath; // 4. analyze request URL // 4.1 prepare a regular expression website1/userindex/yinhuichao RegEx Reg = new RegEx ("/website1/userindex/[A-Za-z0-9 _] + "); // 4.2 determine whether the above regular expression if (Reg. ismatch (urlwithoutpara) {// 4.3 intercept the username parameter website1/userindex? UN = yinhuichao string username = urlwithoutpara. substring (urlwithoutpara. lastindexof ('/') + 1); // 4.4 rewrite context. rewritepath ("../userindex. aspx? UN = "+ username) ;}} public void dispose (){}}

6. Add the reference of the above project class library to the website where you want to use URL rewriting.

7. Modify the website configuration file (Web. config): Add a subnode to the httpmodules node under the system. Web node:

<Add Neme = "urlrewriter" type = "urlrewritermodule. urlrewriter, urlrewritermodule"/>

The above is the so-called URL rewriting usage. The principle used is to process the URL request entered by the user in the httpapplication request pipeline, finally, the user's request URL is handed over to a specific page (with parameters ).

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.