<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
</system.web>
</configuration>
兩個關鍵的類是ModuleRewriter和RewriterFactoryHandler
ModuleRewriter類用於Url重新導向,代碼如下:
ModuleRewriter
using System;
using System.Text.RegularExpressions;
using System.Configuration;
using URLRewriter.Config;
using System.Data;
using System.Web;
using System.Web.UI;
namespace URLRewriter
{
/**//// <summary>
/// Provides a rewriting HttpModule.
/// </summary>
public class ModuleRewriter : BaseModuleRewriter
{
/**//// <summary>
/// This method is called during the module's BeginRequest event.
/// </summary>
/// <param name="requestedRawUrl">The RawUrl being requested (includes path and querystring).</param>
/// <param name="app">The HttpApplication instance.</param>
protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
{
//只對檔案尾碼為aspx頁面有效
if (requestedPath.IndexOf(".aspx") != -1)
{
HttpContext httpContext = app.Context;
// get the configuration rules
RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;
// iterate through each rule
for (int i = 0; i < rules.Count; i++)
{
// get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
string lookFor = "^" + RewriterUtils.ResolveUrl(httpContext.Request.ApplicationPath, rules[i].LookFor) + "$";
// Create a regex (note that IgnoreCase is set)
Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
// See if a match is found
if (re.IsMatch(requestedPath))
{
//aspx頁面重新導向到htm頁面且http資料轉送方式為“GET”
if (rules[i].Type == WebType.Static && app.Context.Request.RequestType == "GET")
{
//靜態頁面路徑
string htmlWeb = RewriterUtils.ResolveUrl(httpContext.Request.ApplicationPath, rules[i].SendTo);
//靜態頁面的最後修改時間
DateTime dt = System.IO.File.GetLastWriteTime(httpContext.Server.MapPath(htmlWeb));
//目前時間和靜態頁面產生時間對比,如果時間小於一個小時,重新導向到靜態頁面
if (DateTime.Compare(DateTime.Now.AddHours(-1), dt) <= 0)
{
requestedPath = htmlWeb;
}
}
else
{
requestedPath = rules[i].SendTo;
}
// Rewrite the URL
RewriterUtils.RewriteUrl(httpContext, requestedPath);
break; // exit the for loop
}
}
}
}
}
}
CreateHtmFactoryHandler類用於產生靜態頁面,代碼如下:
CreateHtmFactoryHandler
using System;
using System.IO;
using System.Web.UI;
using System.Web;
using URLRewriter.Config;
using System.Configuration;
using System.Text.RegularExpressions;
// iterate through each rule
for (int i = 0; i < rules.Count; i++)
{
// get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
string lookFor = "^" + RewriterUtils.ResolveUrl(context.Request.ApplicationPath, rules[i].LookFor) + "$";
// Create a regex (note that IgnoreCase is set)
Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
// See if a match is found
if (re.IsMatch(url) && rules[i].Type == WebType.Static)
{
//得到要產生的靜態頁面的實體路徑
string physicsWeb = context.Server.MapPath(RewriterUtils.ResolveUrl(context.Request.ApplicationPath, rules[i].SendTo));
//設定Response篩選器
context.Response.Filter = new ResponseFilter(context.Response.Filter, physicsWeb);
break;
}
}
}
//得到編譯執行個體
return PageParser.GetCompiledPageInstance(url, pathTranslated, context);
}