
微软公开的URLRewriter源码,配置节点都写在了web.config里,如果你一个网站重写的地址有很多个,那么控制起来应该挺难。所以就在想为什么不把这些节点
写在一个Xml文件里哪?写到Xml文件里又方便控制还可以实现使用方直接配置。
经过查看源码发现我们只要改一个地方就可以实现到Xml文件里,请看下面的两段代码:
微软源码:
public static RewriterConfiguration GetConfig()
{
if (HttpContext.Current.Cache["RewriterConfig"] == null)
HttpContext.Current.Cache.Insert("RewriterConfig", ConfigurationSettings.GetConfig("RewriterConfig"));
return (RewriterConfiguration) HttpContext.Current.Cache["RewriterConfig"];
}
改后过的代码:
public static RewriterConfiguration GetConfig()
{
string key = "Cache:URLRewriter";
if (HttpContext.Current.Cache[key] == null)
{
System.Xml.XmlDocument config = new System.Xml.XmlDocument();
string path = string.Format("{0}\\Bin\\rewrite.xml", HttpContext.Current.Server.MapPath("~/"));
config.Load(path);
object configObj = new RewriterConfigSerializerSectionHandler().Create(null, null, config.SelectSingleNode("configuration"));
HttpContext.Current.Cache.Insert(key, configObj);
}
return (RewriterConfiguration)HttpContext.Current.Cache[key];
}
以上代码存放在RewriterConfiguration.cs文件中。
RewriterConfigSerializerSectionHandler.cs这个文件也有一些改动,修改时只是把IConfigurationSectionHandler 继承去掉了,这里是为了序列化Xml文件,
因为我们现在用的是单独的Xml文件,所以继承可以不用,我是这么想的,所以就去掉了。
本人在Win2003企业版的系统上测试通过,所有文件级使用方式都已经打包。下载
以上仅供参考,本站原创文章,如有转载请带链接注明出自 网络自由人
| #1 | 评论标题:路过看看 | 评论时间:2010-5-10 14:56:26 |
| 路过看看下载了,呵呵,谢谢啊 | ||