How does ASP. NET implement static pages? First, let's look at the source code example of ASP. NET:
- <! -- Main. Aspx -->
- <%@ Page language ="C #"%>
- <%@ ImportNamespace= System. IO %>
- <Script runat ="Server">
- ProtectedOverrideVoidOnInit (EventArgs e)
- {
- IntId;
- Try
- {
- Id =Int. Parse (Request. QueryString ["Id"]);
- }
- Catch
- {
- Throw(NewException ("No id specified for the page"));
- }
- String filename = Server. MapPath ("Statichtml _"+ Id +". Html");
- // Try to read an existing file
- Stream s = GetFileStream (filename );
- If(S! = Null)// If the file exists and is read successfully
- {
- Using(S)
- {
- Stream2Stream (s, Response. OutputStream );
- Response. End ();
- }
- }
- // Call Main_Execute and obtain its output
- StringWriter sw =NewStringWriter ();
- Server. Execute ("Main_Execute.aspx", Sw );
- String content = sw. ToString ();
- // Output to the client
- Response. Write (content );
- Response. Flush ();
- // Write the file
- Try
- {
- Using(FileStream fs =NewFileStream (filename, FileMode. Create, FileAccess. Write, FileShare. Write ))
- {
- Using(StreamWriter streamwriter =NewStreamWriter (fs, Response. ContentEncoding ))
- {
- Streamwriter. Write (content );
- }
- }
- }
- Finally
- {
- // Response. End ();
- }
- }
- Static Public VoidStream2Stream (Stream src, Stream dst)
- {
- Byte [] buf =NewByte [2, 4096];
- While(True)
- {
- IntC = src. Read (buf, 0, buf. Length );
- If(C = 0)
- Return;
- Dst. Write (buf, 0, c );
- }
- }
- PublicStream GetFileStream (string filename)
- {
- Try
- {
- DateTime dt = File. GetLastWriteTime (filename );
- TimeSpan ts = dt-DateTime. Now;
- If(Ts. TotalHours> 1)
- ReturnNull;// Expire in 1 hour
- Return NewFileStream (filename, FileMode. Open, FileAccess. Read, FileShare. Read );
- }
- Catch
- {
- ReturnNull;
- }
- }
- </Script>
-
- <! -- Main_Execute.aspx -->
- <%@ Page language ="C #"%>
- <Html>
- <Head runat ="Server">
- <Title> Untitled Page </title>
- </Head>
- <Body>
- ID:
- <%= Request. QueryString ["Id"] %>
- </Body>
- </Html>
The principle of implementing static pages in ASP. NET is as follows.
Main_Execute.aspx is the HTML page generated.
Now we use Main. aspx to cache it.
ASP. NET implements the static page process as follows:
First, calculate the file name based on the page parameters. (This example is based on Request. QueryString ["id)
Try to read the cached file. If successful, then Response. End ();
If not:
Use Server. Execute to call Main_Execute.aspx and obtain its results.
After the content is obtained, it is immediately output to the client.
Finally, write the content into the file and provide it to the next cache.
The method for implementing static pages in ASP. NET is introduced here. Do you know more about implementing static pages in ASP. NET?
- Cause and solution of ASP. NET Session loss
- ASP. NET code hidden files
- Analysis on the personalization features of ASP. NET basic tutorials
- Analysis of ASP. net html Control Learning
- Procedure for ASP. NET to obtain MAC addresses and IP addresses