aspx页面静态化

IIS设置:站点右键属性,自定义错误,找到404,修改为url,将url设置成/Default.aspx

测试一下:随便敲个地址,比如:http://localhost/123.html,应该跳转到Default.aspx

然后在Default.aspx中写如下逻辑:

View Code
 1        protected void Page_Load(object sender, EventArgs e)
 2         {
 3             if (Request.RawUrl.IndexOf(".html") == -1)
 4             {
 5                 return;
 6             }
 7 
 8 
 9             string filePath = Server.MapPath("/");
10             string[] temp = Request.RawUrl.Split('/');
11             string htmlFile = temp[temp.Length - 1];
12             string aspxFile = htmlFile.Replace(".html", ".aspx");
13 
14 
15             bool b = false;
16 
17 
18             try
19             {
20                 HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://" + Request.Url.Host + ":" + Request.Url.Port.ToString() + "/" + aspxFile);
21                 httpWebRequest.Method = "GET";
22                 using (Stream stream = httpWebRequest.GetResponse().GetResponseStream())
23                 {
24                     using (StreamReader streamReader = new StreamReader(stream, Encoding.UTF8))
25                     {
26                         FileStream fileStream = new FileStream(filePath + htmlFile, FileMode.Create);
27                         byte[] contents = Encoding.Default.GetBytes(streamReader.ReadToEnd());
28                         fileStream.Write(contents, 0, contents.Length);
29                         fileStream.Close();
30 
31 
32                         b = true;
33                     }
34                 }
35             }
36             catch (Exception exception)
37             {
38                 Response.Write(exception.ToString());
39             }
40             finally
41             {
42             }
43 
44 
45             if (b)
46             {
47                 Server.Transfer(htmlFile, true);
48             }
49         }

大概意思是,如果输入的是index.html,那么就会读取index.aspx的动态内容,然后创建index.html

这样第二次访问http://localhost/index.html,就是访问静态页了,然后再做个反向代理(nginx)呗

posted on 2012-06-28 14:21  哥是技术人  阅读(366)  评论(0)    收藏  举报

导航