Today, some people in the group asked me about URL rewriting. If you want to rewrite the URL, remember to read the related articles during the National Day and the msdn introduction. There are three methods, the most common method is to use the DLL file provided by Microsoft. Today, I referenced the DLL and configured the file according to the instructions. Many problems have occurred and cannot be found during debugging. cs source code. I don't know what the problem is. Maybe the DLL has been put for too long .. -_-. #. I had to search for the new DLL online and suddenly found the new method in 2.0! It took me half a night to make a demo!
The demo source code is as follows: the project has only two pages (default. aspx, user. aspx (direct response. Write (request. querystring ["userid"])
Protected void application_beginrequest (Object sender, eventargs E)
{
String suffixurl, lasturl, newurl = string. Empty, userid = string. empty;
Lasturl = request. rawurl;
If (lasturl. substring (0, 11). tolower () = "/urlrewrite") // "urlrewirte is the root directory name, and the reason for judgment is the local test.
{// Retrieved. When uploaded to the server, the root directory will be replaced by the domain name (disappears )!
Suffixurl = lasturl. substring (12). tolower ();
}
Else
{
Suffixurl = lasturl. substring (1). tolower ();
}
Bool ismatch = false;
RegEx myreg = new RegEx (@ "User/([\ W \ D] +) \. aspx ");
If (myreg. ismatch (suffixurl ))
{
Ismatch = true;
Int first = suffixurl. lastindexof ('/') + 1;
Int end = suffixurl. lastindexof ('.');
Int lenght = end-first;
Userid = suffixurl. substring (first, lenght); // obtain the matched group
}
If (ismatch)
{
Newurl = "~ /User. aspx? Userid = "+ userid;
}
Else
{
Newurl = "~ /Default. aspx ";
}
Httpcontext. Current. rewritepath (newurl); // rewrite the URL
}
Test input: http: // xxx/urlrewrite/usre/cyq1_2.aspx (displayed in the address bar)
Successfully directed to: http: // xxx/urlrewrite/user. aspx? Userid = cyq1162 (page content display)
... It's easy... you don't need to introduce DLL or configure webconfig. Just a few lines of code are easy!