Analysis on Static Page implementation using ASP. NET

Source: Internet
Author: User

How does ASP. NET implement static pages? First, let's look at the source code example of ASP. NET:

 
 
  1. <! -- Main. Aspx -->
  2. <%@ Page language ="C #"%>
  3. <%@ ImportNamespace= System. IO %>
  4. <Script runat ="Server">
  5. ProtectedOverrideVoidOnInit (EventArgs e)
  6. {
  7. IntId;
  8. Try
  9. {
  10. Id =Int. Parse (Request. QueryString ["Id"]);
  11. }
  12. Catch
  13. {
  14. Throw(NewException ("No id specified for the page"));
  15. }
  16. String filename = Server. MapPath ("Statichtml _"+ Id +". Html");
  17. // Try to read an existing file 
  18. Stream s = GetFileStream (filename );
  19. If(S! = Null)// If the file exists and is read successfully 
  20. {
  21. Using(S)
  22. {
  23. Stream2Stream (s, Response. OutputStream );
  24. Response. End ();
  25. }
  26. }
  27. // Call Main_Execute and obtain its output 
  28. StringWriter sw =NewStringWriter ();
  29. Server. Execute ("Main_Execute.aspx", Sw );
  30. String content = sw. ToString ();
  31. // Output to the client 
  32. Response. Write (content );
  33. Response. Flush ();
  34. // Write the file 
  35. Try
  36. {
  37. Using(FileStream fs =NewFileStream (filename, FileMode. Create, FileAccess. Write, FileShare. Write ))
  38. {
  39. Using(StreamWriter streamwriter =NewStreamWriter (fs, Response. ContentEncoding ))
  40. {
  41. Streamwriter. Write (content );
  42. }
  43. }
  44. }
  45. Finally
  46. {
  47. // Response. End (); 
  48. }
  49. }
  50. Static Public VoidStream2Stream (Stream src, Stream dst)
  51. {
  52. Byte [] buf =NewByte [2, 4096];
  53. While(True)
  54. {
  55. IntC = src. Read (buf, 0, buf. Length );
  56. If(C = 0)
  57. Return;
  58. Dst. Write (buf, 0, c );
  59. }
  60. }
  61. PublicStream GetFileStream (string filename)
  62. {
  63. Try
  64. {
  65. DateTime dt = File. GetLastWriteTime (filename );
  66. TimeSpan ts = dt-DateTime. Now;
  67. If(Ts. TotalHours> 1)
  68. ReturnNull;// Expire in 1 hour 
  69. Return NewFileStream (filename, FileMode. Open, FileAccess. Read, FileShare. Read );
  70. }
  71. Catch
  72. {
  73. ReturnNull;
  74. }
  75. }
  76. </Script>
  77.  
  78. <! -- Main_Execute.aspx -->
  79. <%@ Page language ="C #"%>
  80. <Html>
  81. <Head runat ="Server">
  82. <Title> Untitled Page </title>
  83. </Head>
  84. <Body>
  85. ID:
  86. <%= Request. QueryString ["Id"] %>
  87. </Body>
  88. </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?

  1. Cause and solution of ASP. NET Session loss
  2. ASP. NET code hidden files
  3. Analysis on the personalization features of ASP. NET basic tutorials
  4. Analysis of ASP. net html Control Learning
  5. Procedure for ASP. NET to obtain MAC addresses and IP addresses

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.