怎么将aspx网站改为静态页面?

简介: 首先思路是在后台点击“生成静态页面” 转换为静态页面 protected void Button2_Click(object sender, EventArgs e) { DataSet ds = Getyuqian().

首先思路是在后台点击“生成静态页面” 转换为静态页面

protected void Button2_Click(object sender, EventArgs e)

{
    DataSet ds = Getyuqian().GetList(0, "", "Tdate desc");//取列表数据

    string str="";
    if (ds != null && ds.Tables[0].Rows.Count > 0)
    {
        for (int i=0; i < ds.Tables[0].Rows.Count-1; i++)//循环ID
        {

            str = ds.Tables[0].Rows[i]["id"].ToString();
            System.IO.StringWriter swHtml = new System.IO.StringWriter();
            Server.Execute("../../yuqiand.aspx?id="+str, swHtml);//获取该数据生成页面的html
            String file1 = str.Substring(0,5)+".html";//去ID前5位为页面名字,因为我把ID加密了比较长
            string contentStr = swHtml.ToString();

            string filePath = Server.MapPath("/");//获取网站本地的基目录
            if (!System.IO.Directory.Exists(filePath))
            {
                System.IO.Directory.CreateDirectory(filePath);
            }
            //如果存在该页面就删除
            foreach (string file in System.IO.Directory.GetFiles(filePath))
            {
                if (file.Substring(file.Length-10,10) == file1)
                {
                    File.Delete(file);
                }

            }
            filePath += file1;
            //按UTF-8导入,不然乱码
            System.IO.StreamWriter sWrite = new System.IO.StreamWriter(filePath, true, Encoding.GetEncoding("UTF-8"));
            sWrite.Write(contentStr);
            sWrite.Flush();
            sWrite.Close();
        }
    }
}

后来我想,添加数据和修改数据就自动生成页面了,那么,使用次数就比较多就些个类来调用把

调用页面:

    Boolean boolx = false;
    Helper.Tohtml htmlto = new Helper.Tohtml();
    boolx = htmlto.writehtml("../../zyd.aspx?id=", model.id.ToString());  
    if (boolx == true)
    {
     //   AlertResponse("发布成功!", "list_qy.aspx");
    }

类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.IO;
using System.Text;
using System.ComponentModel;

namespace Helper
{

/// <summary>
///html 的摘要说明
/// </summary>
public class Tohtml
{
    
 

    public  Boolean writehtml(string url,string str)
    {

        System.IO.StringWriter swHtml = new System.IO.StringWriter();
        HttpContext.Current.Server.Execute(url + str, swHtml);
       // System.Web.HttpUtility.HtmlEncode(url + str); 
        String file1 = str.Substring(0, 5) + ".html";
        string contentStr = swHtml.ToString();

        //string filePath = Server.MapPath("/");
        string filePath = HttpContext.Current.Server.MapPath("/");
        if (!System.IO.Directory.Exists(filePath))
        {
            System.IO.Directory.CreateDirectory(filePath);
        }
        //如果存在该页面就删除
        foreach (string file in System.IO.Directory.GetFiles(filePath))
        {
            if (file.Substring(file.Length - 10, 10) == file1)
            {
                File.Delete(file);
            }

        }
        filePath += file1;

        System.IO.StreamWriter sWrite = new System.IO.StreamWriter(filePath, true, Encoding.GetEncoding("UTF-8"));
        sWrite.Write(contentStr);
        sWrite.Flush();
        sWrite.Close();
        return true;
    }
}

}

OK,可以调用了,除了自己更改就不用点生成就OK了

相关文章
|
11天前
|
搜索推荐 前端开发 UED
html页面实现自动适应手机浏览器(一行代码搞定)
html页面实现自动适应手机浏览器(一行代码搞定)
16 0
|
8月前
|
缓存 Apache C++
phpcms 配置伪静态页面
phpcms 配置伪静态页面
218 0
修改浏览器主页的代码!!!!!!!!!!!!!!!
修改浏览器主页的代码!!!!!!!!!!!!!!!
102 0
|
PHP
织梦显示模板的PHP代码
织梦显示模板的PHP代码
87 0
|
XML PHP 数据格式
thinkphp在iis7以上如何配置伪静态隐藏index.php
thinkphp在iis7以上如何配置伪静态隐藏index.php
272 0
|
应用服务中间件 PHP nginx
php的nginx站点域名后面输入d.aspx跳转至当前页面的index.php
php的nginx站点域名后面输入d.aspx跳转至当前页面的index.php
116 0
|
.NET Windows 开发框架