httphandler配合URLRewrite做url参数隐藏

global.asax.cs文件
        protected void Application_BeginRequest(Object sender, EventArgs e)
         {
              try
              {
                   string path=Server.MapPath("~/ReWriter.config");
                   XPathDocument myXPathDocument = new XPathDocument(path);
                   XPathNavigator myXPathNavigator = myXPathDocument.CreateNavigator();
 
                   XPathNodeIterator myXPathNodeIterator =  myXPathNavigator.Select ("//rule");
                   System.Text.RegularExpressions.Regex oReg;
                   string ReWriteUrl;
 
                   while (myXPathNodeIterator.MoveNext())
                   {
                       //oReg=new Regex(oNode.SelectSingleNode("url/text()").Value);
                       XPathNavigator nav2 = myXPathNodeIterator.Current.Clone();
                       string oldString="",newString="";
                       XPathNodeIterator it2 = nav2.Select("old");
                       while(it2.MoveNext())
                       {
                            oldString = it2.Current.Value;
                            break;
                       }
                       it2 = nav2.Select("new");
                       while(it2.MoveNext())
                       {
                            newString = it2.Current.Value;
                            break;
                       }
                       if(oldString != "" &&  newString != "")
                       {
                                 oReg = new System.Text.RegularExpressions.Regex(oldString);
                                 if(oReg.IsMatch(Request.Url.ToString()))
                                 {
                                     ReWriteUrl = oReg.Replace(Request.Url.ToString(),newString);
                                     HttpContext.Current.RewritePath(ReWriteUrl);
                                     break;
                                 }
                       }
                   }
                  
              }
              catch
              {
              }
 
         }
ReWriter.config内容(配制ReWriter信息)
         
               (.*)/TestUrlRe/file(.*)/(.*)/.html
               ../WebForm1.aspx?id=$2&type=$3
         
         
               (.*)/TestUrlRe/t(.*)/(.*)/.html
               ../WebForm1.aspx?tid=$2&ttype=$3
         
 

你可能感兴趣的:(ASP.NET)