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

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

作者:武眉博<活靶子.NET> 
同时首发于:
    落伍者  && 博客园  
    开发者学院  && .Net男孩社区
知识点: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重写规则
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
部分代码:

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;
            }
        }

转载于:https://www.cnblogs.com/answercard/archive/2009/02/19/1393603.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值