都是在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
|
{
|
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;
|
|
双人厨房做饭手游
双人厨房做饭使你可以在手机上也能体验做饭的乐趣,你可以邀请朋
时光公主中文版(time princess)
时光公主timeprincess是一款元气满满的少女公主换装
泡泡小镇游乐园完整版
泡泡小镇游乐园完整版是可以解锁丰富的游乐园设施的小游戏,大家
串串大师游戏
串串大师对于喜欢美食经营的玩家来说是非常受欢迎的。游戏中你将
不休的乌拉拉最新版
厌倦了江湖的打打杀杀,才发现原来留下的才是最美好的,如今,幡