3. Understand the running mechanism of the page class (for example, generating static files in the render method)

Source: Internet
Author: User

Here I only write a few common events
1. OnPreinit: this event will load personalized information and themes
2. OnInit: initializes the default value of the server control on the page, but the control status is not loaded. No control tree is created.
3. OnPreload: the completion status of the control and the loading of the returned data
4. page_load: this event is onSubscribed in init
5. Render: displays the final page content

Assume that there isArticleDatabase
Previously, articles. aspx was used? Id = 123 dynamic access
Now we want to reduce the pressure on the server and generate static files for the article.
First look at the articles. aspxProgram

Using System;
Using System. collections;
Using System. configuration;
Using System. DaTa;
Using System. text;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. htmlcontrols;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. IO; // Stringwriter namespace

Namespace _ 1
{
Public   Partial   Class Article: system. Web. UI. Page
{
Protected   Void Page_load ( Object Sender, eventargs E)
{
If ( ! String . Isnullorempty (request [ " ID " ])
Label1.text =   " The content of this article is: " + Request [ " ID " ]. Tostring ();
}

Protected   Override   Void Render (htmltextwriter writer)
{
Stringwriter SW =   New Stringwriter (); // This is not much different from stringbuilder.
Htmltextwriter htmlw =   New Htmltextwriter (SW );
Base . Render (htmlw ); // No writer passed in
Htmlw. Flush ();
Htmlw. Close ();
String Pagecontent = Sw. tostring ();
String Path = Server. mappath ( " ~ /Article/ " );
String Pageurl = Xland. mymodule. getfilename (httpcontext. Current );
Using (Streamwriter stringwriter = File. appendtext (Path + Pageurl ))
{
Stringwriter. Write (pagecontent );
}
Response. Write (pagecontent );
}
}
}

We still use custom httpmodules to rewrite the URL.
The webconfig file remains unchanged.

<? XML version = "1.0" ?>
< Configuration >
< System. Web >
< Compilation Debug = "True" > </ Compilation >
< Httpmodules >
< Add Name = "Mymodule" Type = "Xland. mymodule"   />
</ Httpmodules >
</ System. Web >
</ Configuration >

Mymodule Program

Using System;
Using System. Collections. Generic;
Using System. Web; // Reference a web namespace
Using System. text;
Using System. IO;

Namespace Xland
{
Public   Class Mymodule: ihttpmodule
{
Public   Void Init (httpapplication context)
{
Context. beginrequest + = New Eventhandler (context_beginrequest );
}
Public   Void Context_beginrequest ( Object Sender, eventargs E)
{
Httpapplication = (Httpapplication) sender;
Httpcontext Context = Application. context;
// Apprelativecurrentexecutionfilepath does not include the passed parameters.
If (Context. Request. apprelativecurrentexecutionfilepath. tolower (). endswith ( " . Aspx " ))
{
String Fileurl =   " ~ /Article/ "   + Getfilename (context );
If (File. exists (context. server. mappath (fileurl )))
{
Context. rewritepath (fileurl, False );
}
}
}
Public   Static   String Getfilename (httpcontext context)
{
Return Context. Request. apprelativecurrentexecutionfilepath. tolower (). Replace ( " . Aspx " , "" ). Replace ( " ~ / " , "" ) + Context. Request. url. query. Replace ( " ? Id = " , " _ " ) +   " . Html " ;
}
Public   Void Dispose (){}
}
}

There will be no more comments. I believe you can understand them.

This example is only used to describe the page class render event.
Exercise caution when using the project.
Because it will cause a large number of server Io
This is not the best solution for generating static pages.

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.