asp.net 2.0 url rewrite simple implementation

Today, the group I was asked questions about Url rewrite, rewrite think it, I remember reading related articles during the National Day, msdn introduced have seen, probably three methods, the most common is dll file provided by Microsoft, today, the dll I quote a bit, according to explain a bit file configuration, resulting in many problems, can not find the .cs source code debugging, I do not know what problems that may put the dll too long, students show up .. . -_- #, and then had to search the Internet about new dll, 2.0 years suddenly found new ways to spend half the night to do a demo!!
the following is a demo source code: the project only two pages (Default.aspx, user .aspx (direct Response.Write (Request.QueryString [ "the userId"]))
protected void the Application_BeginRequest (SENDER Object, EventArgs E)
    {
        String suffixUrl, lastUrl, newURL = string.Empty, the userId = string.Empty;
        lastUrl = the Request. RawUrl;
        IF (lastUrl.Substring (0,. 11) .ToLower () == "/ UrlRewrite") // "urlrewirte is the root directory name, plus the reason is that the local test determines
         {// is taken out, uploaded to the server when the root is The domain substitution (disappearance)!
            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);//获取被匹配的组部分
        }
        if (isMatch)
        {
           newurl="~/user.aspx?userId="+userId;
         }
        else
          {
            = newURL "~ / the Default.aspx";
           }
 
        HttpContext.Current.RewritePath (newURL); // most critical word, rewriting the Url
    }
test input: http: //xxx/urlrewrite/usre/cyq1162.aspx (Address column display)
successfully directed to: http: //xxx/urlrewrite/user.aspx userid = cyq1162 (page content display)?
.. very simple .. without introducing dll, do not configure Webconfig easy to get a few lines of code.!

Reproduced in: https: //my.oschina.net/secyaher/blog/274143

Guess you like

Origin blog.csdn.net/weixin_33767813/article/details/93713424