asp.net
ASP.net's Rul rewrite
The article is just an idea for rewriting URLs. A succession of MS Components "Urlrewriter" and "Application_BeginRequest ()" Encoding in Global.asax, as well as ISAPI settings in IIS.
Donna listed, the implementation method is also very simple.
Method One: Ms components
Here also do not need to explain, the relevant see:
Http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx
The usage is simple, just copy the component URLRewriter.dll to the application's Bin directory, and then add the following code under Web.config:
Add in <configuration></configuration>:
<configSections>
<section name= "Rewriterconfig" type= "URLRewriter.Config.RewriterConfigSerializerSectionHandler, Urlrewriter"/ >
</configSections>
<RewriterConfig>
<Rules>
<RewriterRule>
<lookfor>~/(\d{4})/(\d{2})/default\.aspx</lookfor>
<SendTo>~/Default.aspx?ID=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
Then add in <system.web></system.web>:
<add verb= "*" Path= "*.aspx"
Type= "Urlrewriter.rewriterfactoryhandler, Urlrewriter"/>
Finally, type on the address bar: http://localhost/Test/2004/12/News.aspx
The effect came out.
The above <lookfor>~/(\d{4})/(\d{2})/news\.aspx</lookfor> This regular expression URL, which is the overridden URL, and <sendto>~/ Default.aspx?id=$1</sendto> This sentence is the original URL address. Where is the first regular expression value (the example above is: 2004), and so on, the second is $
Method Two: Application_BeginRequest ()
Create a new XML file in your application that contains the file name: Rewriter.config
<?xml version= "1.0" encoding= "Utf-8"?>
<ReWriterUrls>
<rule>
<old> (. *)/news/(\d{4})/default\.aspx</old>
<new> /.. /default.aspx?id=$2&type=$3</new>
</rule>
</ReWriterUrls>
In the Global.asax file, add the code in the Application_BeginRequest (Object sender, EventArgs e):
Try
{
String Path=server.mappath ("~/rewriter.config");
Xpathdo***ent myxpathdo***ent = new Xpathdo***ent (path);
XPathNavigator myxpathnavigator = Myxpathdo***ent. 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
{
}