很多朋友可能都討論過ASP.NET中生成HTML的方法了,有按照模板用IO方法寫文件
有在404錯誤頁面內生成HTML的,有在Render內保存頁面輸出到HTML文件的。
今天我發(fā)一個配合Url重寫利用HttpHander手工編譯.aspx頁面方法。
HTML文件的方法,可按需、“定時”的生成,以減輕數(shù)據庫的訪問。
聲明:下面的文字是本文不可缺少的部分,轉載請保留,謝謝!
作者:武眉博<活靶子.NET>
同時首發(fā)于:
????落伍者?? &&?博客園??
????開發(fā)者學院?? && .Net男孩社區(qū)
知識點:UrlRewrite、 IHttpModule、 IHttpHander 的編寫
效果:
http://www.devedu.com/Doc/DotNet/AspNet/default.2.aspx
http://www.devedu.com/Doc/DotNet/AspNet/default.2.html
思路:
1 掛載“.aspx"的請求到自定義的Httphander內
2 配置URL重寫規(guī)則
3 訪問某.aspx文件時,在HttpHander內 根據配置確定是否應該生成
?接著...
?if(需要生成)
?{
??if(若已經生成html文件 )
??{
???if(文件并未過期)
???{
????則直接定向(Server.Transfer())。
???}
???else
???{
????刪除HTML文件;
????重新編譯.aspx(Page內數(shù)據庫操作等等)
????生成HTML文件;
???}
??}
??else if(尚未生成文件)
??{
???生成Html。
??}
?}
?else
?{
??則編譯.aspx文件
?}
另:建議閱讀一下dudu的blog中關于asp.net頁面編譯的討論
http://www.cnblogs.com/dudu/archive/2006/03/07/345107.html
http://www.cnblogs.com/dudu/archive/2006/03/07/344351.html
部分代碼
C#代碼
public?void?ProcessRequest(HttpContext?context) ??????????{ ??????????????string?rawUrl?=?context.Request.RawUrl; ??????????????string?requestPath?=?context.Request.Path; ??????????????string?applicationPath?=?context.Request.ApplicationPath; ??????????????Url?urlItem?=?null; ????????????????????????????????????????????if?(context.Items["ToStaticUrlItem"]?==?null) ??????????????{ ??????????????????if?(!File.Exists(context.Request.PhysicalPath)) ??????????????????{ ??????????????????????throw?new?HttpException(404,?"您訪問的頁面沒有找到。"); ??????????????????} ????????????????????????????????????????????????????????IHttpHandler?hander?=?BuildManager.CreateInstanceFromVirtualPath(requestPath,?typeof(Page))?as?IHttpHandler; ??????????????????hander.ProcessRequest(context); ????????????????????return; ??????????????} ????????????????string?filePath; ????????????????urlItem?=?(Url)context.Items["ToStaticUrlItem"]; ????????????????Regex?regex?=?new?Regex( ??????????????????Globals.ApplicationPath?+?urlItem.LookFor, ??????????????????RegexOptions.CultureInvariant?|?RegexOptions.Singleline?|?RegexOptions.Compiled?|?RegexOptions.IgnoreCase); ????????????????string?requestFile?=?regex.Replace(rawUrl,?Globals.ApplicationPath?+?urlItem.WriteTo.Replace("^",?"&")); ????????????????if?(requestFile.IndexOf("?")?>?0) ??????????????{ ??????????????????filePath?=?requestFile.Substring(0,?requestFile.IndexOf("?")); ??????????????} ??????????????else??????????????{ ??????????????????filePath?=?requestFile; ??????????????} ????????????????string?inputFile?=?context.Request.PhysicalApplicationPath?+?filePath; ??????????????string?path?=?context.Request.PhysicalApplicationPath?+?rawUrl.ToLower().Replace(".aspx",?".html"); ??????????????if?(applicationPath?!=?"/") ??????????????{ ??????????????????inputFile?=?inputFile.Replace(applicationPath?+?"/",?@"\"); ?????????????????path?=?path.Replace(applicationPath?+?"/",?"").Replace("/",?@"\"); ?????????????} ?????????????else ?????????????{ ?????????????????path?=?path.Replace("/",?@"\"); ?????????????} ??????????????if?(!urlItem.EnabledToStatic) ?????????????{ ?????????????????//?asp.net?1.1?采用下面方法編譯頁面 ?????????????????//PageParser.GetCompiledPageInstance(?filePath?,?inputFile?,?context?).ProcessRequest(?context?); ?????????????????IHttpHandler?hander?=?BuildManager.CreateInstanceFromVirtualPath(filePath,?typeof(Page))?as?IHttpHandler; ?????????????????hander.ProcessRequest(context); ??????????????????return; ?????????????} ??????????????if?(!File.Exists(path)) ?????????????{ ?????????????????//?asp.net?1.1?采用下面方法編譯頁面 ?????????????????//PageParser.GetCompiledPageInstance(?filePath?,?inputFile?,?context?).ProcessRequest(?context?); ?????????????????IHttpHandler?hander?=?BuildManager.CreateInstanceFromVirtualPath(filePath,?typeof(Page))?as?IHttpHandler; ?????????????????hander.ProcessRequest(context); ?????????????????context.Response.Filter?=?new?AspxBoy.BuildHtmlDemo.ToHtmlFilter(context.Response.Filter,?path); ??????????????????return; ?????????????} ??????????????if?(urlItem.Minutes?==?Int32.MaxValue) ?????????????{ ?????????????????context.Server.Transfer(rawUrl.ToLower().Replace(".aspx",?".html")); ?????????????} ?????????????else ?????????????{ ?????????????????FileInfo?fileInfo?=?new?FileInfo(path); ?????????????????if?(fileInfo.LastWriteTime.AddMinutes((double)urlItem.Minutes)?<?DateTime.Now) ?????????????????{ ?????????????????????fileInfo.Delete(); ??????????????????????//?asp.net?1.1?采用下面方法編譯頁面 ?????????????????????//PageParser.GetCompiledPageInstance(?filePath?,?inputFile?,?context?).ProcessRequest(?context?); ?????????????????????IHttpHandler?hander?=?BuildManager.CreateInstanceFromVirtualPath(filePath,?typeof(Page))?as?IHttpHandler; ?????????????????????hander.ProcessRequest(context); ?????????????????????context.Response.Filter?=?new?AspxBoy.BuildHtmlDemo.ToHtmlFilter(context.Response.Filter,?path); ?????????????????} ?????????????????else ?????????????????{ ?????????????????????context.Server.Transfer(rawUrl.ToLower().Replace(".aspx",?".html")); ??????????????????} ??????????????????return; ??????????????} ??????????}?? 示例項目下載:http://files.cnblogs.com/huobazi/BuildHtmlDemo.rar
轉載于:https://www.cnblogs.com/huobazi/archive/2007/12/31/UrlRewriteAndHttpHanderAndMakeStaticHtmlFiles.html
總結
以上是生活随笔為你收集整理的[2007最后一博]Url地址重写,利用HttpHander手工编译页面并按需生成静态HTML文件...的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。