zoukankan      html  css  js  c++  java
  • 页面执行时生成静态文件的方法

        protected override void Render(HtmlTextWriter writer)
            
    {
                
    if (EnableStaticFile)
                
    {
                    
    if (transferToHtml) return;
                    
    string authorPath = Server.MapPath("/authors/" + GetUrlPath(authorID) + ".html");
                    
    string authorDir = Path.GetDirectoryName(authorPath);
                    
    if (!Directory.Exists(authorDir))
                    
    {
                        Directory.CreateDirectory(authorDir);
                    }

                    TextWriter ioWriter 
    = null;
                    HtmlTextWriter hioWriter 
    = null;
                    
    try
                    
    {
                        ioWriter 
    = new StreamWriter(authorPath, false, Response.ContentEncoding);
                        hioWriter 
    = new HtmlTextWriter(ioWriter);
                        
    base.Render(hioWriter);
                    }

                    
    catch (Exception) { }
                    
    finally
                    
    {
                        
    if (ioWriter != null) ioWriter.Close();
                        
    if (hioWriter != null) hioWriter.Close();
                    }

                }


                
    base.Render(writer);
            }
    如上代码,重写Page的Render方法,新建一个指向文件的HtmlTextWriter在页面输出之前执行base.Render(txtWriter)即可。非常简单。
  • 相关阅读:
    自定义可下拉刷新列表
    在listview里面的checkbox被选中或取消
    [转]Android中音乐文件的信息详解【安卓源码解析二】
    service 和broadcastreceiver的简略小结
    闹铃
    hdu 2112赤裸裸的最短路
    hdu 2177错题,很水的测试数据
    hdu 2184模拟
    hdu1150最小点集覆盖
    hdu 3746KMP的应用
  • 原文地址:https://www.cnblogs.com/yukaizhao/p/dot_net_generate_static_file.html
Copyright © 2011-2022 走看看