Asp.netOfURLRewrite
AboutURLThis article is just an idea. SuccessivelyMS"Urlrewriter "and"Application_beginrequest () "encoding method, and ISAPI settings in IIS.
The implementation method is also very simple.
Method 1: MS components
You do not need to go into details here. For details, refer:
Http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx
Easy to use, just put the componentsUrlrewriter. dllCopy to ApplicationProgramOfBinDirectory, and thenWeb. configAdd the followingCode:
In<Configuration> </Configuration>Add:
<Configsections>
<SectionName= "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<System. Web> </System. Web>Add:
<Httphandlers>
<AddVerb= "*"Path= "*. Aspx"
Type= "Urlrewriter. rewriterfactoryhandler, urlrewriter"/>
</Httphandlers>
Finally, enter http: // localhost/test/2004/12/news. aspx in the address bar.
The result is displayed.
The above<Lookfor>~ /("D {4})/(" d {2})/News ". aspx</Lookfor>This regular expression URL is the URL to be rewritten.<Sendto>~ /Default. aspx? Id = $1</Sendto> This sentence is the original URL address. $1 indicates the value of the First Regular Expression (2004 in the preceding example), and so on. The second is $2.
Method 2:Application_beginrequest ()
Create an XML file in the application with the file name rewriter. config
<?XMLVersion= "1.0"Encoding= "UTF-8"?>
<Rewriterurls>
<Rule>
<Old>(. *)/News/("d {4})/Default". aspx</Old>
<New>Http://www.cnblogs.com/Default.aspx? Id = $2& Amp;Type = $3</New>
</Rule>
</Rewriterurls>
In the global. asax fileApplication_beginrequest (Object sender, eventargs E) is added to the Code:
Try
{
StringPath = server. mappath ("~ /Rewriter. config ");
Xpathdocument myxpathdocument =NewXpathdocument (PATH );
Xpathnavigator myxpathnavigator = myxpathdocument. createnavigator ();
Xpathnodeiterator myxpathnodeiterator = myxpathnavigator. Select ("// rule ");
System. Text. regularexpressions. RegEx oreg;
StringRewriteurl;
While(Myxpathnodeiterator. movenext ())
{
// Oreg = new RegEx (onode. selectsinglenode ("url/text ()"). value );
Xpathnavigator nav2 = myxpathnodeiterator. Current. Clone ();
StringOldstring = "", 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 =NewSystem. Text. regularexpressions. RegEx (oldstring );
If(Oreg. ismatch (request. url. tostring ()))
{
Rewriteurl = oreg. Replace (request. url. tostring (), newstring );
Httpcontext. Current. rewritepath (rewriteurl );
Break;
}
}
}
}
Catch
{
}