zoukankan      html  css  js  c++  java
  • 动态请求页面生成静态页

    话说百度也是这样提高浏览速度的;此案例是控件开发的,MVC或一般处理程序开发的小伙伴们,修修改改一样可以用的;

    后台:

     1   protected void btnHtml_Click(object sender, EventArgs e)
     2         {
     3             if (Request.QueryString["id"] != null)
     4             {
     5                 string NewId = Request.QueryString["id"];
     6                 CJRZ.Model.Admin.LoanStrategy modeStrategy = bllNews.GetModel(int.Parse(NewId));
     7                 if (modeStrategy != null)
     8                 {
     9                     CreatHtml cHtml = new CreatHtml();
    10                     string url = "http://" + HttpContext.Current.Request.Url.Host.ToString() + ":" + HttpContext.Current.Request.Url.Port;
    11                     string AUrl = url + "/raiders/raiders_article.aspx?id=" + modeStrategy.NewID;
    12                     //html页面文件名
    13                     string fna = Server.MapPath("\") + "raiders\" + modeStrategy.NewTypeID + "\";
    14                     string FileName = modeStrategy.NewID + ".html";
    15                     if (cHtml.CreateList(AUrl, fna, FileName))
    16                     {
    17                         modeStrategy.HtmlUrl = url + "/raiders/" + modeStrategy.NewTypeID + "/" + FileName;
    18                         hlkLook.NavigateUrl = modeStrategy.HtmlUrl;
    19                         if (bllNews.Update(modeStrategy))
    20                         {
    21                             MessageBox.Show(this, "生成文件成功");
    22                         }
    23                         else
    24                         {
    25                             MessageBox.Show(this, "更新数据路径失败!");
    26                         }
    27                     }
    28                     else
    29                     {
    30                         MessageBox.Show(this, "生成文件失败!");
    31                     }
    32                 }
    33                 else
    34                 {
    35                     MessageBox.Show(this, "获取新闻对象失败!");
    36                 }
    37 
    38             }
    39             else
    40             {
    41                 MessageBox.Show(this, "获取ID号失败!");
    42             }
    43         }

    前台:

    1   <asp:Button ID="btnHtml" runat="server" Text="生成静态页" onclick="btnHtml_Click" Visible="false" />
    2                         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    3                            <asp:HyperLink ID="hlkLook" runat="server" style=" color:Blue; font-size:14px;" Target="_blank" >查看页面</asp:HyperLink>

    调用的类:

     1   public class CreatHtml
     2     {
     3         public bool CreateList(string url, string fna,string fileName)
     4         {
     5             bool ok;
     6             //准备生成
     7             string strHtml;
     8             StreamReader sr = null; //用来读取流
     9             StreamWriter sw = null; //用来写文件
    10             Encoding code = Encoding.GetEncoding("utf-8"); //定义编码
    11 
    12             //构造web请求,发送请求,获取响应
    13             WebRequest HttpWebRequest = null;
    14             WebResponse HttpWebResponse = null;
    15             HttpWebRequest = WebRequest.Create(url);
    16             HttpWebResponse = HttpWebRequest.GetResponse();
    17 
    18             //获得流
    19             sr = new StreamReader(HttpWebResponse.GetResponseStream(), code);
    20             strHtml = sr.ReadToEnd();
    21 
    22             //写入文件
    23             try
    24             {
    25 
    26                 if (!Directory.Exists(fna)) 
    27                 {
    28                     Directory.CreateDirectory(fna); 
    29                 }
    30                 sw = new StreamWriter(fna+"\"+fileName, false, code);
    31                 sw.WriteLine(strHtml);
    32                 sw.Flush();
    33                 ok = true;
    34             }
    35             catch (Exception ex)
    36             {
    37                 ok = false;
    38             }
    39             finally
    40             {
    41                 if (sw != null)
    42                 {
    43                     sw.Close();
    44                 }
    45             }
    46             return ok;
    47         }
    48     }
  • 相关阅读:
    二元函数求一定区间上的最大值问题
    LOF异常检测算法实现
    失效项目总代码汇总
    layui弹出层layer.open 中的content问题
    分治算法基本原理和实践
    Android 文件存储浅析
    详解 CmProcess 跨进程通信的实现
    View Animation 运行原理解析
    margin-top失效及解决办法
    vue自定义移动端touch事件之点击、滑动、长按事件
  • 原文地址:https://www.cnblogs.com/woloveprogram/p/4955415.html
Copyright © 2011-2022 走看看