要解決這個問題,我們需要先了解ASP.NET應用程序的生命周期,先看下面作者整理的一張圖片:
從圖中我們可以清楚的看到:通用IIS訪問應用程序時,每次的單個頁面URL訪問時,都會先經過HttpApplication 管線處理請求,走過BeginRequest 事件之后才會去走路由訪問具體的Controller和Action,最后結束的時候會請求EndRequest事件。下面用一張圖來表示這個順序:
"HelloWorldModule: End of Request</font></h1>");
}
}
public void Dispose() { }
}
復制代碼 代碼如下:
<httpModules>
<add type="MvcTest.MyHandler"/>
</httpModules>
復制代碼 代碼如下:
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"))
{
//判斷緩存是否存在,不存在加入緩存,調用生成靜態的類和方法
//產品過期,移除靜態文件,302重定向
if (System.IO.File.Exists(context.Server.MapPath(filePath)))
{
context.Response.WriteFile(context.Server.MapPath(filePath));
context.Response.End();
}
}
新聞熱點
疑難解答
圖片精選