Url地址重写,利用HttpHander手工编译页面并按需生成静态HTML文件

翻译|其它|编辑:郝浩|2008-01-02 13:39:56.000|阅读 1189 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

思路:
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#代码
public void ProcessRequest(HttpContext context)   
        {   
            string rawUrl = context.Request.RawUrl;   
            string requestPath = context.Request.Path;   
            string applicationPath = context.Request.ApplicationPath;   
            Url urlItem = null;   
  
            //上下文中没有定义 ToStaticUrlItem 表示,此请求没有经过 UrlRewrite,直接编译,不生成 html   
            //参考 UrlRewriteModule.cs   
            if (context.Items["ToStaticUrlItem"] == null)   
            {   
                if (!File.Exists(context.Request.PhysicalPath))   
                {   
                    throw new HttpException(404, "您访问的页面没有找到。");   
                }   
  
                // asp.net 1.1 采用下面方法编译页面   
                //PageParser.GetCompiledPageInstance(requestPath, context.Request.PhysicalPath, context).ProcessRequest(context);   
                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;   
            }   
        }  


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:.NET博客园

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP