利用模版asp.net生成静态页面

本文介绍了一种利用ASP.NET动态生成静态HTML页面的方法。通过一个包含标题和内容输入框的简单界面,用户可以定制静态页面的内容,并将其保存为HTML文件。此过程涉及从前端收集数据、替换模板文件中的占位符以及将最终结果写入新的HTML文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模版页(我把里面放了一个table,页面有2个标记 title1和content)-----------------------------------------------------------------------------------

<!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>title1</title>
</head>
<body>
    <table style="width: 522px">
        <tr>
            <td>
            </td>
            <td>
            </td>
            <td style="width: 170px">
            </td>
        </tr>
        <tr>
            <td style="height: 21px">
           
            </td>
            <td style="height: 21px">
            </td>
            <td style="width: 170px; height: 21px">
            </td>
        </tr>
        <tr>
            <td style="height: 130px">内容:content
           
            </td>
            <td style="height: 130px">
            </td>
            <td style="width: 170px; height: 130px">
            </td>
        </tr>
    </table>

</body>
</html>

前台页面asp.aspx(有textbook1和textbook2分别用于设置生静态页面的title和content)---------------------------------------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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 runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="标题" Width="101px"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server" Width="171px"></asp:TextBox>
       

        <asp:Label ID="Label3" runat="server" Text="内容" Width="99px"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server" Height="175px" TextMode="MultiLine"></asp:TextBox>

       

        &nbsp;<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="生成静态页面" /></div>
    </form>
</body>
</html>

后台代码-(主要是button的事件代码)-------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Net;

public partial class Default2 : System.Web.UI.Page
{

    protected void Button1_Click(object sender, EventArgs e)
    {
        string mypath = Server.MapPath("template.htm");
        Encoding code = Encoding.GetEncoding("gb2312");
        StreamReader sr = null;
        StreamWriter sw = null;
        string str = "";

        //把模版页读入到数据流
        try
        {
            sr = new StreamReader(mypath, code);
            str = sr.ReadToEnd();
        }
        catch (Exception)
        {
            Response.Write("错误01");
        }
        finally
        {
            sr.Close();
        }
        // 使用系统时间自动生成文件名
        string fileName = System.DateTime.Now.ToString("yyyyMMddhhmmss") + ".htm";

        //更改页面的title 和 内容
        str = str.Replace("title1", TextBox1.Text);
        str = str.Replace("content", TextBox2.Text);

        //写入到指定htm文件
        try
        {
            sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
            sw.Write(str);
            sw.Flush();
        }
        catch (Exception)
        {
            Response.Write("错误02");
        }
        finally
        {
            sw.Close();

            Response.Write("<a href = htm/" + fileName + "  target=_black>" + fileName + "");
        }
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值