asp.net generate static page notes _ Practical Tips

Source: Internet
Author: User
Tags flush
1. Use Serever. Excute
Copy Code code 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 code 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 code 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" onclick= "Button1_Click" 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= "generated from URL address" onclick= "button2_click"/></div>
</form>
</body>

Default.aspx.cs
Copy Code code 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!" ");
}
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.