熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

將.aspx轉換為.htm的兩種方法

2022-06-13   來源: .NET編程 
這篇文章介紹了將aspx轉換為htm的兩種方法有需要的朋友可以參考一下希望對你有所幫助  

  方法一:根據模板生成保持在html文件夾中
思路分析:
寫一個自定義的HTM模板  其中需要替換的地方用$value$這樣
包含起來  
生成頁面的ASPX中用StreamReader讀取HTM模板用REPLACE
替換$value$  
把完成的字符串用StreamWriter輸出
參考代碼如下:
)定義模板emplatehtm

復制代碼 代碼如下:
<!DOCTYPE html PUBLIC "//WC//DTD XHTML Transitional//EN" "
<html xmlns="
<head>
    <title> $title$ 生成靜態頁的Demo|aspxcom</title>
    <style type="text/css">
<!
STYLE {
fontsize: px;
fontweight: bold;
}
>
    </style>
</head>
<body>
<br />
<br />
<table width="%" border="" bgcolor="#">
  <tr>
    <td height="" align="center" bgcolor="#FFFFFF"><span class="STYLE">$title$ </span></td>
  </tr>
  <tr>
    <td height="" bgcolor="#FFFFFF"><br />
      <br />
    內容$content$ </td>
  </tr>
</table>
<a href="#" target="_blank">版權所有</a>
</body>
</html>

  
)在Defaultaspx頁面的按扭的事件處理中寫如下代碼:

復制代碼 代碼如下:
//源碼是替換掉模板中的特征字符
  string mbPath = ServerMapPath("templatehtm");
  Encoding code = EncodingGetEncoding("gb");
  StreamReader sr = null;
  StreamWriter sw = null;
  string str = null;
  //讀取
  try
  {
  sr = new StreamReader(mbPath code);
  str = srReadToEnd();
  }
  catch (Exception ex)
  {
  throw ex;
  }
  finally
  {
  srClose();
  }
  //根據時間自動重命名擴展名也可以自行修改
  string fileName = DateTimeNowToString("yyyyMMddHHmmss") + "htm";
  str = strReplace("$title{}quot; txtTitleText);//替換Title
  str = strReplace("$content{}quot; txtContentText);//替換content
  //生成靜態文件
  try
  {
  sw = new StreamWriter(ServerMapPath("htm/") + fileName false code);
  swWrite(str);
  swFlush();
  }
  catch (Exception ex)
  {
  throw ex; 
  }
  finally
  {
  swClose();
  ResponseWrite("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已經生成保存在htm文件夾下!");
  }

  
方法二:根據Url地址生成靜態頁保持
思路分析:
直接將做好的動態頁面翻譯成靜態頁面所以生成的內容不夠靈活
參考代碼:

復制代碼 代碼如下:

  
//根據Url地址生成靜態頁保持
protected void Button_Click(object sender EventArgs e)
{
  Encoding code = EncodingGetEncoding("utf");
            StreamReader sr = null;
            StreamWriter sw = null;
            string str = null;
            //讀取遠程路徑
            WebRequest temp = WebRequestCreate(txtUrlTextTrim());
            WebResponse myTemp = tempGetResponse();
            sr = new StreamReader(myTempGetResponseStream() code);
            //讀取
            try
            {
                sr = new StreamReader(myTempGetResponseStream() code);
                str = srReadToEnd();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                srClose();
            }
            string fileName = DateTimeNowToString("yyyyMMddHHmmss") + "htm";
            //寫入
            try
            {
                sw = new StreamWriter(ServerMapPath("htm/") + fileName false code);
                swWrite(str);
                swFlush();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                swClose();
                ResponseWrite("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已經生成保存在htm文件夾下!");
            }
        }   


From:http://tw.wingwit.com/Article/program/net/201311/14153.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.