Asp.net generate static page notes

Source: Internet
Author: User

1. Use serever. Excute Copy codeThe 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. Replace characters

Url rewriting
1. Define Rewrite Rules
Urls. xml is changed to urls. configCopy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Urls>
<Rewrite name = "ShowArticle" pattern = "article-(\ d000000000000.html" path = "article-000000000.html" page = "showarticle. aspx "query =" id = $1 "> </rewrite>
<Rewrite name = "ShowList" pattern = "list-(\ d000000000000.html" path = "list-000000000.html" page = "showlist. aspx "query =" id = $1 "> </rewrite>
</Urls>

2. Create a simple object urls class
3. The urls class obtains all urls in the urls. config file.
4. httpmodule class handles the request address
5. On the web. config httpmodule node, add

Asp.net: two methods for generating static pages

Default. aspx page:Copy codeThe 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">
<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 = "button#click" Text = "generated by template"/> <br/>
<Br/>
<Br/>
Url address: <asp: TextBox ID = "txtUrl" runat = "server" ToolTip = "Make sure the Url address exists" Width = "359px"> </asp: TextBox>
<Br/>
<Br/>
<Asp: Button ID = "Button2" runat = "server" Text = "generate" OnClick = "Button2_Click"/> </div>
</Form>
</Body>
</Html>

Default. aspx. csCopy codeThe 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 button#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 ();
}
// Automatically rename the file according to the time, and the extension can be modified by yourself
String fileName = DateTime. Now. ToString ("yyyyMMddHHmmss") + ". htm ";
Str = str. Replace ("$ title $", txtTitle. Text); // Replace the 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 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 the 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 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.