要解決這個(gè)問題,我們需要先了解ASP.NET應(yīng)用程序的生命周期,先看下面作者整理的一張圖片:

從圖中我們可以清楚的看到:通用IIS訪問應(yīng)用程序時(shí),每次的單個(gè)頁面URL訪問時(shí),都會(huì)先經(jīng)過HttpApplication 管線處理請(qǐng)求,走過BeginRequest 事件之后才會(huì)去走路由訪問具體的Controller和Action,最后結(jié)束的時(shí)候會(huì)請(qǐng)求EndRequest事件。下面用一張圖來表示這個(gè)順序:
"HelloWorldModule: End of Request</font></h1>");
}
}
public void Dispose() { }
}
復(fù)制代碼 代碼如下:
<httpModules>
<add type="MvcTest.MyHandler"/>
</httpModules>
復(fù)制代碼 代碼如下:
private void Application_BeginRequest(Object source,
EventArgs e)
{
// Create HttpApplication and HttpContext objects to access
// request and response properties.
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
string filePath = context.Request.FilePath;
string fileExtension =
VirtualPathUtility.GetExtension(filePath);
if (fileExtension.Equals(".html"))
{
//判斷緩存是否存在,不存在加入緩存,調(diào)用生成靜態(tài)的類和方法
//產(chǎn)品過期,移除靜態(tài)文件,302重定向
if (System.IO.File.Exists(context.Server.MapPath(filePath)))
{
context.Response.WriteFile(context.Server.MapPath(filePath));
context.Response.End();
}
}
新聞熱點(diǎn)
疑難解答
圖片精選