参考文章
http://www.cnblogs.com/edobnet/archive/2004/06/02/12866.aspx
参考代码
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&type=$3</new>
</rule>
<rule>
<old>(.*)/TestUrlRe/t(.*)/(.*).html</old>
<new>../WebForm1.aspx?tid=$2&ttype=$3</new>
</rule>
</ReWriterUrls>
<ReWriterUrls>
<rule>
<old>(.*)/TestUrlRe/file(.*)/(.*).html</old>
<new>../WebForm1.aspx?id=$2&type=$3</new>
</rule>
<rule>
<old>(.*)/TestUrlRe/t(.*)/(.*).html</old>
<new>../WebForm1.aspx?tid=$2&ttype=$3</new>
</rule>
</ReWriterUrls>
http://www.cnblogs.com/edobne