I forgot how the source code came from. It seems to be from Microsoft. Later I thought the configuration was too cumbersome and inconvenient to use.
I changed it slightly. I will report it at the beginning. You will give me some comments.
Usage:
1.
Web. config
Add:
<? XML version = "1.0"?>
<Configuration>
<System. Web>
<Httpmodules>
<Add name = "modulerewriter" type = "jiaolg. urlrewriter. modulerewriter, urlrewriter"/>
</Httpmodules>
</System. Web>
</Configuration>
2.
Copy urlrewriter. DLL to the bin directory.
3.
Copy rewriter. XML to app_data
Some source code: UsingSystem;
UsingSystem. Web;
UsingSystem. Web. caching;
UsingSystem. xml;
UsingSystem. Text. regularexpressions;
Namespace Jiaolg. urlrewriter
{
Public Class Modulerewriter: basemodulerewriter
{
Private Static Readonly String Cname = " Rules " ;
Private Xmlnodelist rules;
protected override void rewrite ( string requestedpath, system. web. httpapplication APP)
{< br> app. context. trace. write ( " modulerewriter " , " entering modulerewriter " );
If (Httpruntime. cache [cname] = Null )
{
Cachedependency CD = New Cachedependency (rules. configfile );
Rules = New Rules (). getrules ();
Httpruntime. cache. insert (cname, rules, Cd );
}
Else
{
Rules = (Xmlnodelist) httpruntime. cache [cname];
}
// Iterate through each rule
Foreach (Xmlnode rule In Rules)
{
String Lf = Rule. firstchild. selectsinglenode ( " Lookfor " ). Innertext;
String St = Rule. firstchild. selectsinglenode ( " Sendto " ). Innertext;
String Lookfor = " ^ " + Rewriterutils. resolveurl (App. Context. Request. applicationpath, LF) + " $ " ;
RegEx re= NewRegEx (lookfor, regexoptions. ignorecase );
If (Re. ismatch (requestedpath)
{< br> string sendtourl = rewriterutils. resolveurl (App. context. request. applicationpath, re. replace (requestedpath, St);
app. context. trace. write ( " modulerewriter " , " rewriting URL to " + sendtourl );
rewriterutils. rewriteurl (App. context, sendtourl);
Break ;
}< BR >}
App. Context. Trace. Write ("Modulerewriter","Exiting modulerewriter");
}
}
}
Download (including source code ):
Urlrewriter.zip