现在的位置: 首页 > 综合 > 正文

ASP.NET 动态生成静态页面

2013年02月13日 ⁄ 综合 ⁄ 共 3570字 ⁄ 字号 评论关闭

asp.net 生成静态页面思路
1.建立一个空的模板页面
2.建立一个aspx页面去读取模板信息,并替换相关的资料
3.写入/htm产生新的htm

--------------------------------------------------------Template--------------------------------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

    <title>$title$ </title>
<meta http-equiv="Content-Typ" content="text/html;charset=Unicode(UTF-8)"   />
    <link href="../../css/Sytel.css" rel="stylesheet" type="text/css" />
     <link href="../css/Sytel.css" rel="stylesheet" type="text/css" />
</head>

<body>


    <div id="DIV1" class="bb bai" style="left: 0px; width: 98%; top: 1px; height: 339px;">
        <div class="rb1">
        </div>
        <div class="rb2">
        </div>
        <div class="t1" style="font-family: 新細明體">
            <span style="font-size: 10pt">$class{1}lt;/span></div>
        <div  class="bc" style="text-align: left">
           <table width="99%" border="0">

  <tr>

    <td    style="width: 904px;height:34px; text-align: center;" class="h"><span class="STYLE1">$title$ </span></td>

  </tr>

  <tr>

    <td style="text-align: center" >
        $content$
    </td>

  </tr>
               <tr>
                   <td  style="text-align: right; height: 30px;">
                       $name{1}lt;br />
                       $date$
                   </td>
               </tr>

</table></div>
        <div class="bai">
            <div class=" rb3">
            </div>
            <div class="rb4">
            </div>
        </div>
    </div>

</body>

</html>

---------------------------------------------------------aspx-------------------------------------------------------------

using System;
using System.Collections;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Net;
using System.Data.SqlClient;

public partial class test_ExcuteHtml : System.Web.UI.Page
{
  
    protected void Page_Load(object sender, EventArgs e)
    {
        string sql = string.Empty;
       // string strName =Session["login_name"].ToString().Trim();
        
        string strDate = DateTime.Now.ToString("yyyy年MM月dd日HH:mm");
        /*
        string strTitle =string.Empty;
        if (Request.Form["titlez"] != null) { strTitle = Request.Form["titlez"].ToString().Trim(); }
        string strContent =string.Empty;
        if (Request.Form["bodyz"] != null) { strContent = Request.Form["bodyz"].ToString().Trim(); }
        string strClass = string.Empty;
        if (Request.Form["cla"] != null) { strClass = Request.Form["cla"].ToString().Trim(); }
         */
        string  strTitle = "我是标题";
        string strClass = "我是类别";
        string strContent = "我是内容";
        string strName = "名字";
        //根据实际情况填写
       // Encoding Code = Encoding.GetEncoding("Big5");

        Encoding Code = Encoding.GetEncoding("GB2312");
        

        StreamReader Sr = StreamReader.Null;

        StreamWriter Sw = StreamWriter.Null;

        String Str = String.Empty;
        //读取远程路径
       string Sessionpath = Session["Path"].ToString().Trim();

        WebRequest temp = WebRequest.Create(Sessionpath + "template.htm");

        WebResponse myTemp = temp.GetResponse();

        Sr = new StreamReader(myTemp.GetResponseStream(), Code);

        //读取

        try
        {

            Sr = new StreamReader(myTemp.GetResponseStream(), Code);


            Str = Sr.ReadToEnd();


        }

        catch (Exception ex)
        {

            throw ex;

        }

        finally
        {

            Sr.Close();

        }

        string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm"; //文件名


        //写入

        try
        {

            Sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, Code);
            Str = Str.Replace("$class{1}quot;, strClass);
            Str = Str.Replace("$title{1}quot;, strTitle);
            Str = Str.Replace("$content{1}quot;, strContent);
            Str = Str.Replace("$name{1}quot;,strName);
            Str = Str.Replace("$date{1}quot;,strDate);

            Sw.Write(Str);

            Sw.Flush();


        }

        catch (Exception ex)
        {

            throw ex;

        }

        finally
        {

            Sw.Close();
            strTitle = "<a href=../../Form/G_it/News/htm/" + fileName + " target=_blank>" + strTitle + "</a>";
            //sql = "INSERT   INTO    [News1] (classz,titlez,namez,datez) VALUES('" + strClass + "','" + strTitle + "', '" + strName + "','" + strDate + "')";
            //string ch = nb.ExecSQL(sql).ToString().Trim();
           // ch = ch.ToLower();
           // Response.Write("{success:true,ch:" + ch + "}");
        }
    }

}

 

抱歉!评论已关闭.