FileMaker:
public class FileMaker
{
public FileMaker()
{ }
private static bool NeedMake(string AppPath, int timeout)
{
//如果文件不存在,就生成它。
if (File.Exists(AppPath) == false) return true;
//没启用超时,不生成。
if (timeout == 0) return false;
//如果过期,生成。
DateTime dt = File.GetLastWriteTime(AppPath);
if (dt.AddMinutes(timeout) <= DateTime.Now)
return true;
return false;
}
/// <summary>
/// Make
/// </summary>
/// <param name="url"></param>
/// <param name="toPath"></param>
public static void Make(string url,int timeout)
{
HttpContext context = HttpContext.Current;
string AppPath = context.Request.PhysicalPath;
if (NeedMake(AppPath, timeout) == false) return;
StreamWriter sw = new StreamWriter(AppPath, false, Encoding.GetEncoding("GB2312"));
try
{
context.Server.Execute(url, sw);
}
catch {
}
sw.Close();
}
}
HttpModule :
public class HttpModule:IHttpModule
{
public void Init(HttpApplication app)
{
app.BeginRequest += new EventHandler(this.ModuleRewriter_BeginRequest);
}
public void ModuleRewriter_BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication) source;
HttpContext context = application.Context;
string newPath = null;
string path = context.Request.Path;
int timeout;
bool isReWritten = RewriteUrl(path, out newPath, out timeout);
if (isReWritten && newPath != null)
{
FileMaker.Make(newPath, timeout);
}
}
public static bool RewriteUrl(string path,out string newPath,out int timeout)
{
SiteUrls siteurl = Global.GetSiteUrls();//具体实现不在描述
ArrayList urls = siteurl.ReWrittenUrls;//具体实现不在描述
if (urls.Count > 0)
{
foreach (ReWrittenUrl url in urls)
{
if (url.IsMatch(path))
{
newPath = url.Convert(path);
timeout = url.TimeOut;
return true;
}
}
}
newPath = null;
timeout = 0;
return false;
}
}
URLCONFIG.CONFIG
ForExample:
<?xml version="1.0" encoding="utf-8" ?>
<urls>
<!--
****************
注意, 在url查询字符串中,字符 ^ 将被字符 & 替换.
****************
-->
<url name="cat_show" timeout="20" pattern="~/Cat/(/d+).aspx" vanity="~/SrvPort/class_list.aspx?class_id=$1"/>
<url name="aritcle_show" timeout="0" pattern="~/File/(/d+).aspx" vanity="~/SrvPort/article_show.aspx?id=$1"/>
</urls>