我的,URL ReWrite代码

 

Global.aspx.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 ,配制文件

<?xml version="1.0" encoding="utf-8" ?>
<ReWriterUrls>
        
<rule>
            
<old>(.*)/TestUrlRe/file(.*)/(.*).html</old>
            
<new>../WebForm1.aspx?id=$2&amp;type=$3</new>
        
</rule>
        
<rule>
            
<old>(.*)/TestUrlRe/t(.*)/(.*).html</old>
            
<new>../WebForm1.aspx?tid=$2&amp;ttype=$3</new>
        
</rule>
</ReWriterUrls> 
posted on 2004-07-02 13:05  edobnet  阅读(2458)  评论(3编辑  收藏  举报