Asp.net static page instance

Source: Internet
Author: User

Protected Void Button#click ( Object Sender, eventargs E)
{
String URL = " Http :// " + Request. url. Authority + " /Default. aspx " ;
New System. net. WebClient (). downloadfile (URL, server. mappath ( " ~ /Default.html " ));
Response. Redirect ( " Default.html " );
}

BeforeArticle: Asp.net static page generation demo

ThisCodeThe solution is also written based on the Code of many people. I would like to express my gratitude to these unsung contributors.

Paste a core code:

 Using  System;
Using System. Web;
Using System. Web. UI;
Using System. IO;
Using System. text;
Using System. Text. regularexpressions;
Using System. configuration;

Namespace Bll
{
/// <Summary>
/// Abstract description of pagebase.
/// </Summary>
Public Class Pagebase: Page
{
Private Static Object OBJ = New Object ();

/// / <Summary>
/// Override the default htmltextwriter method, modify the value attribute in the form tag, and set the value to the rewritten URL instead of the real URL.
/// </Summary>
/// <Param name = "Writer"> </param>
Protected Override Void Render (htmltextwriter writer)
{
String URL = Request. rawurl. tolower ();
If (Request. requesttype. tolower (). Equals ( " Get " ))
{
Urlrewrite u = (Urlrewrite) httpcontext. Current. items [ " Siteurl " ];
If (U ! = Null && U. Cache > 0 )
{
String Cachefile = Server. mappath ( " /Cache " + URL + " . Html " );

Fileutil. createdirectory (cachefile );

Lock (OBJ)
{
Using (Streamwriter SW = New Streamwriter (cachefile, False , Encoding. utf8 ))
{

Base . Render ( New Fetchhtmlwriter (writer, SW ));
Sw. Flush ();
Sw. Close ();
Sw. Dispose ();
}
Cache. Add (URL, String . Empty, Null , Datetime. Now. addseconds (U. cache), timespan. Zero, system. Web. caching. cacheitempriority. High, Null );
Return ;
}
}
}
If (Writer Is System. Web. UI. html32textwriter)
{
Writer = New Formfixerhtml32textwriter (writer. innerwriter );
}
Else
{
Writer = New Formfixerhtmltextwriter (writer. innerwriter );
}

Base . Render (writer );
}
}

Public Class Formfixerhtml32textwriter: system. Web. UI. html32textwriter
{
Public Formfixerhtml32textwriter (textwriter writer)
: Base (Writer)
{
}

Public Override Void Writeattribute ( String Name, String Value, Bool Encode)
{
// If the current output attribute is the form-marked action attribute, replace the value with the rewritten false URL.
If ( String . Compare (name, " Action " , True ) = 0 )
{
Value = Httpcontext. Current. Request. rawurl;
}
Base . Writeattribute (name, value, encode );
}
}


Public Class Formfixerhtmltextwriter: system. Web. UI. htmltextwriter
{
Public Formfixerhtmltextwriter (textwriter writer)
: Base (Writer)
{
}

Public Override Void Writeattribute ( String Name, String Value, Bool Encode)
{
If ( String . Compare (name, " Action " , True ) = 0 )
{
Value = Httpcontext. Current. Request. rawurl;
}

Base . Writeattribute (name, value, encode );
}
}
}

Download the code in detail: Asp.net generates a static page

If you have any questions, add the QQ group: 49745612 for further discussion.

Compared with the previous method, the method for generating static pages is much more complicated, and I prefer the previous implementation method.

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.