Asp.net URL rewriting
This article is just an idea about URL rewriting. The MS component "urlrewriter" and the "application_beginrequest ()" encoding method in global. asax are successively available, as well as 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
The usage is simple. You only need to copy the urlrewriter. dll component to the application.ProgramIn the bin directory, and then add the following to Web. config:Code:
Add the following to <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>
Add the following content to <system. Web> </system. Web>:
<Httphandlers>
<Add verb = "*" Path = "*. aspx"
Type = "urlrewriter. rewriterfactoryhandler, urlrewriter"/>
</Httphandlers>
Finally, enter http: // localhost/test/2004/12/news. aspx in the address bar.
The result is displayed.
<Lookfor> ~ /(\ D {4})/(\ D {2})/news \. aspx </lookfor> This regular expression URL, that is, the URL to be rewritten, and <sendto> ~ /Default. aspx? Id = $1 </sendto> is the original URL. $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
<? XML version = "1.0" encoding = "UTF-8"?>
<Rewriterurls>
<Rule>
<Old> (. *)/news/(\ D {4})/Default \. aspx </old>
<New>.../default. aspx? Id = $2 & amp; type = $3 </New>
</Rule>
</Rewriterurls>
Add the following code to application_beginrequest (Object sender, eventargs E) in the global. asax file:
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
{
}
From http://www.dezai.cn/index/Article_Show.asp? ArticleID = 6336