ASP.NET產生靜態頁的方法

來源:互聯網
上載者:User

方法1:首先建一個想要產生靜態頁的aspx頁DynamicPage.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DynamicPage.aspx.cs" Inherits="AspAjax._DynamicPage" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    
    </div>
    </form>
</body>
</html>

然後建一個用於產生其它頁面的ExcuteHtml.aspx,並且拖放一個按鈕,然後在CodeBehind檔案中鍵入如下代碼:

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

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

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            GenerateStaticPage(Server.MapPath("html/StaticPage.html"), "DynamicPage.aspx");

        }

        /// <summary>
        /// 產生靜態頁
        /// </summary>
        /// <param name="destPage">產生靜態頁的全路徑名稱</param>
        /// <param name="srcPage">動態網頁</param>
        private void GenerateStaticPage(string destPage, string srcPage)
        {
            StreamWriter sw = new StreamWriter(destPage, false, Encoding.GetEncoding("gb2312"));
            Server.Execute(srcPage, sw);
            sw.Close();
        }

    }
}

方法2:

WebClient wc = new WebClient();
            wc.Encoding = Encoding.UTF8;
            wc.DownloadFile("http://xxx/Default.aspx",Server.MapPath("/1.html"));

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.