asp.net generate static page notes
Last Update:2017-02-28
Source: Internet
Author: User
1. Use Serever. Excute
Copy Code The code is as follows:
StreamWriter sw = New StreamWriter (Server.MapPath ("html/login.html"), false);
Server.Execute ("showcolumn.aspx?id=1&page=2", SW);
Sw. Close ();
2. Substitution characters
URL Rewrite
1. Define rewrite rules
Urls.xml into Urls.config.
Copy Code The code is as follows:
<?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 URL class
The 3.urls class gets all the URLs in the Urls.config file
4.httpmodule class processing the address of the request
5. Add in Web.config HttpModule node
asp.net two ways to generate static pages
Default.aspx page:
Copy Code The code is as follows:
<%@ 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 ">
Two examples of <title>asp.net generating static pages </title>
<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" text= "Generate from template"/><br/>
<br/>
<br/>
URL address: <asp:textbox id= "Txturl" runat= "server" tooltip= "Please confirm the existence of URL address" width= "359px" ></asp:TextBox>
<br/>
<br/>
<asp:button id= "Button2" runat= "server" text= "Generate from URL address"/></div>
</form>
</body>
Default.aspx.cs
Copy Code The code is as follows:
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)
{
Source code is to replace the character 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 ();
}
Automatically renamed according to time, extension can also be modified by itself
String fileName = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". htm";
str = str. Replace ("$title $", txttitle.text);//Replacement title
str = str. Replace ("$content $", txtcontent.text);//replacement content
Generating 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, saved in the HTM 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, saved in the HTM folder!" ");
}
}
}
}