2018-03-20
Default.aspx 內容省略
Default.aspx.cs 內容如下
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Threading; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication2 { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // 請不要轉換自己,否則下面 page.Server.Execute(path, writer); 會出現 「未處理的例外狀況 System.StackOverflowException」 //transHtml("Default.aspx", "Default.htm"); transHtml("WebForm1.aspx", "WebForm1.htm"); } public void transHtml(string path, string outpath) { System.Web.UI.Page page = new Page(); StringWriter writer = new StringWriter(); page.Server.Execute(path, writer); FileStream fs; fs = File.Create(page.Server.MapPath("") + "\\" + outpath); //繁體中文字會亂碼,必須強行指定 UTF8 //byte[] bt = System.Text.Encoding.Default.GetBytes(writer.ToString()); byte[] bt = System.Text.Encoding.UTF8.GetBytes(writer.ToString()); fs.Write(bt, 0, bt.Length); fs.Close(); Response.Write("寫入成功"); } } } |
WebForm1.aspx 內容如下 ( WebForm1.aspx.cs 內容省略)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
測試<br />
TEST
</div>
</form>
</body>
</html>
|
(完)
參考
[Asp.NET]aspx動態網頁轉成html靜態網頁 @ 資訊園 :: 痞客邦 PIXNET ::
http://fecbob.pixnet.net/blog/post/35647209-%5Basp.net%5Daspx%E5%8B%95%E6%85%8B%E7%B6%B2%E9%A0%81%E8%BD%89%E6%88%90html%E9%9D%9C%E6%85%8B%E7%B6%B2%E9%A0%81
沒有留言:
張貼留言