Analysis of ASP. net url rewriting

Source: Internet
Author: User

See http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx

The simplest implementation is to judge and rewrite the request URL in application_beginrequest or application_authenticaterequest event processing in global. asax. CS:

Protected void application_beginrequest (Object sender, eventargs E)

{

Httpapplication APP = (httpapplication) sender;

String requestedpath = app. Request. path;

String lookfor = @ "^/webapptest/urlrewritetest/department/(/W +)/. aspx $ ";

String sendto = "/webapptest/urlrewritetest/webform2.aspx? Dept = $1 ";

RegEx Re = new RegEx (lookfor, regexoptions. ignorecase );

If (Re. ismatch (requestedpath ))

{

String sendtourl = Re. Replace (requestedpath, sendto );

App. Context. rewritepath (sendtourl );

}

}

The rewrite mainly uses the httpcontext. rewritepath method.

The execution result is as follows:

Http: // localhost/webapptest/urlrewritetest/department/finance. aspx

Rewrite:

Http: // localhost/webapptest/urlrewritetest/webform2.aspx? Dept = Marketing

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.