您当前的位置: 首页 >  .net

ASP.NET 动态生成静态页面

发布时间:2008-12-03 08:59:00 ,浏览量:0

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

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

$title$
		
$class{1}lt;/span>
$title$ $content$ $name{1}lt;br /> $date$

---------------------------------------------------------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 = "            
关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    107388博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0495s