自己正在用的url转义控件(带源码)
忘记最初的源码怎么来的了,好像是微软的吧,后来觉得那个配置实在太烦琐了,用起来很是不方便。
略微改了一下,初来报道,大家给个意见
使用方法:
1.
web.config
添加:
<?xml version="1.0"?>
<configuration>
<system.web>
<httpModules>
<add name="ModuleRewriter" type="JIAOLG.URLRewriter.ModuleRewriter, UrlRewriter"/>
</httpModules>
</system.web>
</configuration>
2.
把UrlRewriter.dll复制到bin目录
3.
把rewriter.xml复制到App_Data
部分源码:
略微改了一下,初来报道,大家给个意见
使用方法:
1.
web.config
添加:
<?xml version="1.0"?>
<configuration>
<system.web>
<httpModules>
<add name="ModuleRewriter" type="JIAOLG.URLRewriter.ModuleRewriter, UrlRewriter"/>
</httpModules>
</system.web>
</configuration>
2.
把UrlRewriter.dll复制到bin目录
3.
把rewriter.xml复制到App_Data
部分源码:
using System;
using System.Web;
using System.Web.Caching;
using System.Xml;
using System.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)
{
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 = new Regex(lookFor, RegexOptions.IgnoreCase);
if (re.IsMatch(requestedPath))
{
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;
}
}
app.Context.Trace.Write("ModuleRewriter", "Exiting ModuleRewriter");
}
}
}
using System.Web;
using System.Web.Caching;
using System.Xml;
using System.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)
{
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 = new Regex(lookFor, RegexOptions.IgnoreCase);
if (re.IsMatch(requestedPath))
{
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;
}
}
app.Context.Trace.Write("ModuleRewriter", "Exiting ModuleRewriter");
}
}
}
下载(含源码):
UrlRewriter.zip