惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 活靶子.Net

2008我的收获了3个本儿,一堆朋友 把ASP.NET MVC的路由利用配置维护起来 jQuery从此更牛了 【分享】如何创建自己的Visual Studio 2005/2008配色 我学Linq to SQL做的demo Latest preview of the ASP.Net MVC available on CodePlex 提供我现用的Vs配色(灰黑色调)下载,有兴趣的朋友玩玩。 CustomError可以设置绝对路径 使用AzMan找不到Microsoft.Interop.Security.AzRoles的解决方法 MS Ajax 客户端生命周期 使用dotNetZipLib轻松压缩目录/文件夹 批量 移动 msn 联系人 IE8 BETA1 来了.... Telligent 发布了 CMS 系统 DbEntry.Net---又一个国产开源ORM数据访问及 WEB 框架 FckEditor,远程图片下载,插件 上班族饮食十大“夺命”恶习 一系列SQL SERVER SQL 的分析文章 员工辞职的十大原因!
[2007最后一博]Url地址重写,利用HttpHander手工编译页面并按需生成静态HTML文件
活靶子.Net · 2007-12-31 · via 博客园 - 活靶子.Net


很多朋友可能都讨论过ASP.NET中生成HTML的方法了,有按照模板用IO方法写文件
有在404错误页面内生成HTML的,有在Render内保存页面输出到HTML文件的。
今天我发一个配合Url重写利用HttpHander手工编译.aspx页面方法。
HTML文件的方法,可按需、“定时”的生成,以减轻数据库的访问。

声明:下面的文字是本文不可缺少的部分,转载请保留,谢谢!
////////////////////////////////////////////////////
作者:<活靶子.NET>
同时首发于:
   
 博客园  
    

知识点:UrlRewriteIHttpModuleIHttpHander 的编写
效果:
http://www.网址作废.com/Doc/DotNet/AspNet/default.2.aspx
http://www.网址作废.com/Doc/DotNet/AspNet/default.2.html
思路:
1 挂载“.aspx"的请求到自定义的Httphander内
2 配置URL重写规则
3 访问某.aspx文件时,在HttpHander内 根据配置确定是否应该生成
 接着...
 if(需要生成)
 {
  if(若已经生成html文件 )
  {
   if(文件并未过期)
   {
    则直接定向(Server.Transfer())。
   }
   else
   {
    删除HTML文件;
    重新编译.aspx(Page内数据库操作等等)
    生成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#代码

  1. public void ProcessRequest(HttpContext context)   
  2.         {   
  3.             string rawUrl = context.Request.RawUrl;   
  4.             string requestPath = context.Request.Path;   
  5.             string applicationPath = context.Request.ApplicationPath;   
  6.             Url urlItem = null;   
  7.   
  8.               
  9.               
  10.             if (context.Items["ToStaticUrlItem"] == null)   
  11.             {   
  12.                 if (!File.Exists(context.Request.PhysicalPath))   
  13.                 {   
  14.                     throw new HttpException(404, "您访问的页面没有找到。");   
  15.                 }   
  16.   
  17.                   
  18.                   
  19.                 IHttpHandler hander = BuildManager.CreateInstanceFromVirtualPath(requestPath, typeof(Page)) as IHttpHandler;   
  20.                 hander.ProcessRequest(context);   
  21.   
  22.                 return;   
  23.             }   
  24.   
  25.             string filePath;   
  26.   
  27.             urlItem = (Url)context.Items["ToStaticUrlItem"];   
  28.   
  29.             Regex regex = new Regex(   
  30.                 Globals.ApplicationPath + urlItem.LookFor,   
  31.                 RegexOptions.CultureInvariant | RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);   
  32.   
  33.             string requestFile = regex.Replace(rawUrl, Globals.ApplicationPath + urlItem.WriteTo.Replace("^""&"));   
  34.   
  35.             if (requestFile.IndexOf("?") > 0)   
  36.             {   
  37.                 filePath = requestFile.Substring(0, requestFile.IndexOf("?"));   
  38.             }   
  39.             else  
  40.             {   
  41.                 filePath = requestFile;   
  42.             }   
  43.   
  44.             string inputFile = context.Request.PhysicalApplicationPath + filePath;   
  45.             string path = context.Request.PhysicalApplicationPath + rawUrl.ToLower().Replace(".aspx"".html");   
  46.             if (applicationPath != "/")   
  47.             {   
  48.                 inputFile = inputFile.Replace(applicationPath + "/", @"\");  
  49.                 path = path.Replace(applicationPath + "/", "").Replace("/", @"\");  
  50.             }  
  51.             else  
  52.             {  
  53.                 path = path.Replace("/", @"\");  
  54.             }  
  55.  
  56.             if (!urlItem.EnabledToStatic)  
  57.             {  
  58.                 // asp.net 1.1 采用下面方法编译页面  
  59.                 //PageParser.GetCompiledPageInstance( filePath , inputFile , context ).ProcessRequest( context );  
  60.                 IHttpHandler hander = BuildManager.CreateInstanceFromVirtualPath(filePath, typeof(Page)) as IHttpHandler;  
  61.                 hander.ProcessRequest(context);  
  62.  
  63.                 return;  
  64.             }  
  65.  
  66.             if (!File.Exists(path))  
  67.             {  
  68.                 // asp.net 1.1 采用下面方法编译页面  
  69.                 //PageParser.GetCompiledPageInstance( filePath , inputFile , context ).ProcessRequest( context );  
  70.                 IHttpHandler hander = BuildManager.CreateInstanceFromVirtualPath(filePath, typeof(Page)) as IHttpHandler;  
  71.                 hander.ProcessRequest(context);  
  72.                 context.Response.Filter = new AspxBoy.BuildHtmlDemo.ToHtmlFilter(context.Response.Filter, path);  
  73.  
  74.                 return;  
  75.             }  
  76.  
  77.             if (urlItem.Minutes == Int32.MaxValue)  
  78.             {  
  79.                 context.Server.Transfer(rawUrl.ToLower().Replace(".aspx", ".html"));  
  80.             }  
  81.             else  
  82.             {  
  83.                 FileInfo fileInfo = new FileInfo(path);  
  84.                 if (fileInfo.LastWriteTime.AddMinutes((double)urlItem.Minutes) < DateTime.Now)  
  85.                 {  
  86.                     fileInfo.Delete();  
  87.  
  88.                     // asp.net 1.1 采用下面方法编译页面  
  89.                     //PageParser.GetCompiledPageInstance( filePath , inputFile , context ).ProcessRequest( context );  
  90.                     IHttpHandler hander = BuildManager.CreateInstanceFromVirtualPath(filePath, typeof(Page)) as IHttpHandler;  
  91.                     hander.ProcessRequest(context);  
  92.                     context.Response.Filter = new AspxBoy.BuildHtmlDemo.ToHtmlFilter(context.Response.Filter, path);  
  93.                 }  
  94.                 else  
  95.                 {  
  96.                     context.Server.Transfer(rawUrl.ToLower().Replace(".aspx", ".html"));   
  97.                 }   
  98.                 return;   
  99.             }   
  100.         }  

示例项目下载:https://files.cnblogs.com/huobazi/BuildHtmlDemo.rar