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