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     }
  • 相关阅读:
    如何用Warkdowm来写一篇博客
    关于toString()的一些事情
    python的 range() 函数
    python中的split()函数
    innerHTML和value打架了?
    JS如何实现实时获取网络时间
    javascript内嵌样式与外联样式怎么做?
    c语言实现乘法口诀表
    c语言实现数字的倒序输出
    c语言解一元二次方程
  • 原文地址:https://www.cnblogs.com/woloveprogram/p/4955415.html
Copyright © 2011-2022 走看看