都是在web.config里配置访问规则在路由到实际页面,所以先实现自定义节点信息读取,这里我定义了俩个类,贴出详细信息
namespace UrlRewrite |
{ |
public class UrlRewriteConfigGroup : ConfigurationSectionGroup |
{ |
[ConfigurationProperty( "rules" )] |
public UrlRewriteConfig Rules |
{ |
get |
{ |
return (UrlRewriteConfig) base .Sections[ "rules" ]; |
} |
} |
} |
} |
namespace UrlRewrite |
{ |
public class UrlRewriteConfig : ConfigurationSection |
{ |
[ConfigurationProperty( "enable" , DefaultValue = false )] |
public bool Enable |
{ |
get |
{ |
return ( bool ) base [ "enable" ]; |
} |
} |
|
[ConfigurationProperty( "suffix" , DefaultValue = ".html" )] |
public string Suffix |
{ |
get |
{ |
return ( string ) base [ "suffix" ]; |
} |
} |
|
|
[ConfigurationProperty( "" , IsDefaultCollection = true )] |
public RulesElectionCollection Rules |
{ |
get |
{ |
return (RulesElectionCollection) base [ "" ]; |
} |
} |
|
} |
|
public class UrlRewriteConfigRule : ConfigurationElement |
{ |
[ConfigurationProperty( "form" )] |
public string Form |
{ |
get |
{ |
return ( string ) base [ "form" ]; |
} |
} |
[ConfigurationProperty( "to" )] |
public string To |
{ |
get |
{ |
return ( string ) base [ "to" ]; |
} |
} |
[ConfigurationProperty( "query" )] |
public string Query |
{ |
get |
{ |
return ( string ) base [ "query" ]; |
} |
} |
} |
|
public class RulesElectionCollection : ConfigurationElementCollection |
{ |
protected override ConfigurationElement CreateNewElement() |
{ |
return new UrlRewriteConfigRule(); |
} |
|
protected override object GetElementKey(ConfigurationElement element) |
{ |
return ((UrlRewriteConfigRule)element).Form; |
} |
} |
|
public static class UrlRewriteConfigHelp |
{ |
public static bool Enabled |
{ |
get |
{ |
UrlRewriteConfig section |
= ConfigurationManager.GetSection( "rulesGroup/rules" ) |
as UrlRewriteConfig; |
if (section == null ) |
{ |
return false ; |
} |
return section.Enable; |
} |
} |
|
public static string Suffix |
{ |
get |
{ |
UrlRewriteConfig section |
= ConfigurationManager.GetSection( "rulesGroup/rules" ) |
as UrlRewriteConfig; |
if (section == null ) |
{ |
return ".html" ; |
} |
return section.Suffix; |
} |
} |
|
|
public static RulesElectionCollection Rules |
{ |
get |
{ |
UrlRewriteConfig section |
= ConfigurationManager.GetSection( "rulesGroup/rules" ) |
as UrlRewriteConfig; |
if (section == null ) |
{ |
throw new System.InvalidOperationException( "未能获取rulesGroup/rules" ); |
} |
return section.Rules; |
} |
} |
|
public static string RewriteBase |
{ |
get |
{ |
string path = HttpContext.Current.Request.ApplicationPath; |
if (!path.EndsWith( "/" )) |
{ |
return path + "/" ; |
} |
|
return path; |
} |
} |
|
} |
|
} |
namespace UrlRewrite |
{ |
public class UrlRewrite : IHttpModule |
{ |
|
#region IHttpModule 成员 |
|
public void Dispose() |
{ |
throw new NotImplementedException(); |
} |
|
public void Init(HttpApplication context) |
{ |
context.BeginRequest += new EventHandler(Application_BeginRequest); |
//context.PreRequestHandlerExecute += new EventHandler(Application_PreRequestHandlerExecute); |
} |
|
void Application_BeginRequest( object sender, EventArgs e) |
{ |
HttpContext context = HttpContext.Current; |
|
// 取得当前请求的路径 |
string path = context.Request.Path; |
|
string suffix = String.Empty; |
if (UrlRewriteConfigHelp.Enabled) |
suffix = UrlRewriteConfigHelp.Suffix; |
|
// 遍历所有的映射规则,进行映射处理 |
foreach (UrlRewriteConfigRule rule in UrlRewriteConfigHelp.Rules) |
{ |
Regex regex = new Regex( "^" + rule.Form + suffix + "$" , RegexOptions.IgnoreCase); |
Match match = regex.Match(path); |
if (match.Success) |
{ |
string trueUrl = UrlRewriteConfigHelp.RewriteBase + rule.To; |
|
context.Items[ "OriginalUrl" ] = context.Request.RawUrl; |
|
if ( "POST" .Equals(context.Request.HttpMethod)) |
trueUrl = trueUrl.Substring(0, trueUrl.LastIndexOf( "?" )); |
else |
{ |
int count = match.Groups.Count; |
|
if (count > 1 && match.Groups[1].Value != "" ) |
{ |
for ( int i = 1; i <= count; i++) |
{ |
trueUrl = trueUrl.Replace( "$" + i, match.Groups[i + 1].Value); |
} |
} |
} |
|
context.RewritePath(trueUrl); |
return ; |
} |
} |
|
//context.RewritePath("/error.htm"); |
} |
namespace UrlRewrite |
{ |
public class FormRewriterControlAdapter : |
System.Web.UI.Adapters.ControlAdapter |
{ |
protected override void Render(HtmlTextWriter writer) |
{ |
base .Render( new RewriteFormHtmlTextWriter(writer)); |
} |
} |
|
public class RewriteFormHtmlTextWriter : HtmlTextWriter |
{ |
public RewriteFormHtmlTextWriter(HtmlTextWriter writer) |
: base (writer) |
{ |
this .InnerWriter = writer.InnerWriter; |
} |
|
public RewriteFormHtmlTextWriter(TextWriter writer) |
: base (writer) |
{ |
this .InnerWriter = writer; |
} |
|
public override void WriteAttribute( string name, string value, bool fEncode) |
{ |
if (name == "action" ) |
{ |
HttpContext context = HttpContext.Current; |
|