Simple URL rewriting in Asp.net 2.0

Source: Internet
Author: User

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!

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.