实现IHttpHandlerFactory接口

in Develop

Blog的URL重写是基于IHttpHandlerFactory接口进行的
以前写过一篇”实现IHttpHandler接口实现URL重写”的文章,那篇仅是实现IHttpHandler接口,
将.aspx重写为.Html。但由于要配置IIS,而并不是每个时候都能自行配置IIS,于是没有采用以前的重写方法。

我们都知道,在machine.config文件中的httpHandlers配置节中,.aspx的扩展是由System.Web.UI.PageHandlerFactory的类来处理的。
现在通过实现IHttpHandlerFactory接口,来创建新的IHttpHandler对象.

public class FactoryHandler : IHttpHandlerFactory
{
public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
{
}
public virtual void ReleaseHandler(IHttpHandler handler)
{
}
}

在今天以前,重写规则是配置在web.config中的。今天抽点时间将所有的规则配置至一xml文件中,
并缓存至DataTime.MaxValue,在Cache时做了一个依赖,当xxx.xml被修改时缓存失效。

if (HttpContext.Current.Cache["RewriterConfig"] == null)
{
System.Data.DataSet dst = new System.Data.DataSet();
dst.ReadXml(HttpContext.Current.Server.MapPath("xxx.xml"));
HttpContext.Current.Cache.Insert("RewriterConfig", dst.Tables[0],
new CacheDependency(HttpContext.Current.Server.MapPath("xxx.xml")),
DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.High, null);
}
return HttpContext.Current.Cache["RewriterConfig"] as System.Data.DataTable;

这样一处理,web.config文件小了许多。

0 Comments

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>