Two ways to convert .aspx to .html

怪我咯
Release: 2017-04-01 11:30:48
Original
4038 people have browsed it

This article introduces two methods of converting .aspx to .htm. Friends in need can refer to it. I hope it will be helpful to you.

Method 1: Generate according to the template, Keep it in the html folder
Idea analysis:
1. Write a custom HTM template. Use $value$ to
include the places that need to be replaced.
2. Generate the page In ASPX, use StreamReader to read the HTM template and replace $value$ with REPLACE

3. Put the completed String Use StreamWriter to output
The reference code is as follows:
1) Define template template.htm

The code is as follows:

<!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> $title$ 生成静态页的Demo|-51aspx.com</title> 
    <style type="text/css"> 
<!-- 
.STYLE1 { 
font-size: 16px; 
font-weight: bold; 
} 
--> 
    </style> 
</head> 
<body> 
<br /> 
<br /> 
<table width="100%" border="0" bgcolor="#339900"> 
  <tr> 
    <td height="34" align="center" bgcolor="#FFFFFF"><span class="STYLE1">$title$ </span></td> 
  </tr> 
  <tr> 
    <td height="42" bgcolor="#FFFFFF"><br /> 
      <br /> 
    内容:$content$ </td> 
  </tr> 
</table> 
<a href="#" target="_blank">版权所有</a> 
</body> 
</html>
Copy after login


2) Write the following code in the Event handling of the button on the Default.aspx page:

The code is as follows:

//源码是替换掉模板中的特征字符 
  string mbPath = Server.MapPath("template.htm"); 
  Encoding code = Encoding.GetEncoding("gb2312"); 
  StreamReader sr = null; 
  StreamWriter sw = null; 
  string str = null; 
  //读取 
  try 
  { 
  sr = new StreamReader(mbPath, code); 
  str = sr.ReadToEnd(); 
  } 
  catch (Exception ex) 
  { 
  throw ex; 
  } 
  finally 
  { 
  sr.Close(); 
  } 
  //根据时间自动重命名,扩展名也可以自行修改 
  string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm"; 
  str = str.Replace("$title{1}quot;, txtTitle.Text);//替换Title 
  str = str.Replace("$content{1}quot;, txtContent.Text);//替换content 
  //生成静态文件 
  try 
  { 
  sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code); 
  sw.Write(str); 
  sw.Flush(); 
  } 
  catch (Exception ex) 
  { 
  throw ex;  
  } 
  finally 
  { 
  sw.Close(); 
  Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已经生成,保存在htm文件夹下!"); 
  }
Copy after login


Method 2: Generate static pages based on Url addresses to maintain
Idea analysis:
Directly translate the completed dynamic pages into static pages, so the generated content is not flexible enough
Reference code:

The code is as follows:

//根据Url地址生成静态页保持 
protected void Button2_Click(object sender, EventArgs e) 
{ 
  Encoding code = Encoding.GetEncoding("utf-8"); 
            StreamReader sr = null; 
            StreamWriter sw = null; 
            string str = null; 
            //读取远程路径 
            WebRequest temp = WebRequest.Create(txtUrl.Text.Trim()); 
            WebResponse myTemp = temp.GetResponse(); 
            sr = new StreamReader(myTemp.GetResponseStream(), code); 
            //读取 
            try 
            { 
                sr = new StreamReader(myTemp.GetResponseStream(), code); 
                str = sr.ReadToEnd(); 
            } 
            catch (Exception ex) 
            { 
                throw ex; 
            } 
            finally 
            { 
                sr.Close(); 
            } 
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm"; 
            //写入 
            try 
            { 
                sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code); 
                sw.Write(str); 
                sw.Flush(); 
            } 
            catch (Exception ex) 
            { 
                throw ex; 
            } 
            finally 
            { 
                sw.Close(); 
                Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已经生成,保存在htm文件夹下!"); 
            } 
        }
Copy after login


The above is the detailed content of Two ways to convert .aspx to .html. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!