国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

C#靜態(tài)頁面生成

2024-07-21 02:26:04
字體:
供稿:網(wǎng)友
  • 網(wǎng)站運(yùn)營seo文章大全
  • 提供全面的站長運(yùn)營經(jīng)驗(yàn)及seo技術(shù)!
  • 環(huán)境:microsoft .net framework sdk v1.1
    os:windows server 2003 中文版
    asp.net生成靜態(tài)html頁
    在asp中實(shí)現(xiàn)的生成靜態(tài)頁用到的filesystemobject對(duì)象!
    在.net中涉及此類操作的是system.io
    以下是程序代碼 注:此代碼非原創(chuàng)!參考別人代碼
    //生成html頁
      public static bool writefile(string strtext,string strcontent,string strauthor)
      {
       string path = httpcontext.current.server.mappath("/news/");
       encoding code = encoding.getencoding("gb2312");
       // 讀取模板文件
       string temp = httpcontext.current.server.mappath("/news/text.html");
       streamreader sr=null;
       streamwriter sw=null;
       string str=""; 
       try
       {
        sr = new streamreader(temp, code);
        str = sr.readtoend(); // 讀取文件
       }
       catch(exception exp)
       {
        httpcontext.current.response.write(exp.message);
        httpcontext.current.response.end();
        sr.close();
       }
     
      
       string htmlfilename=datetime.now.tostring("yyyymmddhhmmss")+".html";
       // 替換內(nèi)容
       // 這時(shí),模板文件已經(jīng)讀入到名稱為str的變量中了
       str =str.replace("showarticle",strtext); //模板頁中的showarticle
       str = str.replace("biaoti",strtext);
       str = str.replace("content",strcontent);
       str = str.replace("author",strauthor);
       // 寫文件
       try
       {
        sw = new streamwriter(path + htmlfilename , false, code);
        sw.write(str);
        sw.flush();
       }
       catch(exception ex)
       {
        httpcontext.current.response.write(ex.message);
        httpcontext.current.response.end();
       }
       finally
       {
        sw.close();
       }
       return true;

    此函數(shù)放在conn.cs基類中了
    在添加新聞的代碼中引用 注:工程名為hover
     
        if(hover.conn.writefilethis.title.text.tostring),this.content.text.tostring),this.author.text.tostring)))
        {
         response.write("添加成功");
        }
        else
        {
         response.write("生成html出錯(cuò)!");
        }
    -------------------------------------------------------------------------
    模板頁text.html代碼
    -------------------------------------------------------------------------
    <!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
    <html>
     <head>
      <title>showarticle</title> 
     <body> biaoti
     <br>
     content<br>
     author
     </body>
    </html>------------------------------------------------------------------------
    提示添加成功后會(huì)出以當(dāng)前時(shí)間為文件名的html文件!上面只是把傳遞過來的幾個(gè)參數(shù)直接寫入了html文件中,在實(shí)際應(yīng)用中需要先添加數(shù)據(jù)庫,然后再寫入html文件
    而且需要把生成的文件名等寫入數(shù)庫以便以后調(diào)用等,此實(shí)例只是實(shí)現(xiàn)了根據(jù)提交過來參數(shù)替換模板中的相應(yīng)的字段! 需要完善的地方很多!哪位有高見,歡迎賜教!


     

    發(fā)表評(píng)論 共有條評(píng)論
    用戶名: 密碼:
    驗(yàn)證碼: 匿名發(fā)表
    主站蜘蛛池模板: 突泉县| 开封县| 灵丘县| 弋阳县| 昌江| 盈江县| 沧源| 华宁县| 合川市| 聊城市| 盘山县| 黔西县| 阿拉善盟| 临沭县| 贺州市| 德清县| 休宁县| 遂平县| 从江县| 垫江县| 乐至县| 海晏县| 岳池县| 禹城市| 巴里| 渝北区| 菏泽市| 克东县| 图木舒克市| 桐庐县| 互助| 高淳县| 威海市| 达日县| 正定县| 嘉荫县| 青龙| 汝州市| 牡丹江市| 绍兴市| 兴海县|