asp.net generates static page notes
- 2020-05-12 02:27:53
- OfStack
1. Use serever. Excute
2. Replace characters
url rewrite
1. Define rewrite rules
urls. xml into urls. config
2. Create a simple entity urls class
3. Class urls gets all url in the urls.config file
4. The httpmodule class handles the address of the request
5. Add at the web.config httpmodule node
asp.net two ways to generate static pages
Default. aspx page:
Default.aspx.cs
StreamWriter sw = new StreamWriter(Server.MapPath("html/Login.html"), false);
Server.Execute("ShowColumn.aspx?id=1&page=2", sw);
sw.Close();
2. Replace characters
url rewrite
1. Define rewrite rules
urls. xml into urls. config
<?xml version="1.0" encoding="utf-8" ?>
<Urls>
<rewrite name="ShowArticle" pattern="article-(\d+).html" path ="article-{0}.html" page="showarticle.aspx" query="id=$1"></rewrite>
<rewrite name="ShowList" pattern="list-(\d+).html" path ="list-{0}.html" page="showlist.aspx" query="id=$1"></rewrite>
</Urls>
2. Create a simple entity urls class
3. Class urls gets all url in the urls.config file
4. The httpmodule class handles the address of the request
5. Add at the web.config httpmodule node
asp.net two ways to generate static pages
Default. aspx page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication6._Default" %>
<!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 id="Head1" runat="server">
<title>Asp.net Two examples of generating static pages </title>
</head>
<body>
<form id="form1" runat="server">
<div>
Title: <asp:TextBox ID="txtTitle" runat="server" Width="352px"></asp:TextBox><br />
Content: <asp:TextBox ID="txtContent" runat="server" Height="179px" TextMode="MultiLine"
Width="350px"></asp:TextBox><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text=" Generate from template " /><br />
<br />
<br />
Url Address: <asp:TextBox ID="txtUrl" runat="server" ToolTip=" Please make sure the Url The existence of an address " Width="359px"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button2" runat="server" Text=" According to the Url Address generation " OnClick="Button2_Click" /></div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Text;
using System.IO;
namespace WebApplication6
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
// The source code replaces the feature characters in the template
string mbPath = Server.MapPath("template.htm");
Encoding code = Encoding.GetEncoding("gb2312");
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
// read
try
{
sr = new StreamReader(mbPath, code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
// The name is automatically renamed based on time, and the extension can be changed
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
str = str.Replace("$title$", txtTitle.Text);// replace Title
str = str.Replace("$content$", txtContent.Text);// replace content
// Generate static files
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(" congratulations <a href=htm/" + fileName + " target=_blank>" + fileName + "</a> Has been generated and saved in htm Under the folder! ");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Encoding code = Encoding.GetEncoding("utf-8");
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
// Read remote path
WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());
WebResponse myTemp = temp.GetResponse();
sr = new StreamReader(myTemp.GetResponseStream(), code);
// read
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";
// write
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(" congratulations <a href=htm/" + fileName + " target=_blank>" + fileName + "</a> Has been generated and saved in htm Under the folder! ");
}
}
}
}