日期:2014-05-17  浏览次数:20482 次

学习:生成单个html页面
我想做一个新闻发布系统,在输入新闻标题和内容后生成一个html页面,网上看到很多.net生成静态页面的例子,不过大多看不懂,求一个最基本的语句

------解决方案--------------------
你是想生成一个静态页面还是有页面了只是把新闻内容填充进去。
------解决方案--------------------
静态页面的话可以用文本编辑器生成内容。然后把内容添加上 html的 开头结尾标签。保存为文件.html放在项目中,输入地址就可以看。
如果只是填充新闻的话 ,就把文本编辑器编辑的内容保存到数据库。然后加载页面的时候把内容放到页面div内就可以
------解决方案--------------------
所谓的生成静态页面,简单点说,其实就是制作一个有特殊标签的模板,然后生成的时候把标记替换成你的内容。复杂点你制作一套标签,然后通过正则匹配替换的内容,都是一个道理替换,生成


------解决方案--------------------
引用:
我知道是可以通过html模板,替换其中的内容就可以,我只需要替换html中4个地方就行。具体的不知道怎么做。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Text;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string templateContent = ReadFile("Template/index.htm");

        templateContent = templateContent.Replace("{content}", "新内容").Replace("{title}", "新标题");

        CreateFile("Index.html", templateContent);

    }


    public static void CreateDir(string dir) {
        if (dir.Length == 0) return;
        string path = System.Web.HttpContext.Current.Request.MapPath("/");
        if (!Directory.Exists(path + dir))
            Directory.CreateDirectory(System.Web.HttpContext.Current.Request.MapPath("/") + dir);
    }

    public static Boolean CreateFile(string dir, string pagestr) {

        System.IO.StreamWriter sw = null;
        try {
            sw = new StreamWriter(HttpContext.Current.Request.MapPath("~/") + "\\" + dir, false, Encoding.GetEncoding("GB2312"));
            sw.Write(pagestr);
        }
        catch (Exception ex) {
            sw.Close();
            sw.Dispose();
            throw ex;
        }
      &nbs