asp.net静态化页面

静态化页面是指使用动态页面语言编程,生成*.htm静态页面文件。好像这个技术很流行!看象百度知道,没一个问题都是一个html文件,163新闻评论页面也都是html页面。所以在网上搜索了一下相关的资料。
     为什么都喜欢采用静态页面化?听说是有以下有点:1、 提高服务器性能, *.aspx之类的动态页面毕竟是还要经过服务器解析处理的,而静态页面服务器只要返回相应的文件就可以了。怎么说静态面效率也要高一点啦。2、说是安全性高(可能是说不用担心SQL注入这些东西了,静态页面啊,当然没有这个了)。3、容易被搜索引擎收录(好像网页搜索引擎处理静态页面更容易一点,想也是动态页面还要请求啊什么的)。可能还有其他有点我就不知道了。不过我个人觉得要不要采用静态化网页也还是要考虑一下的,动态网页的性能也没有象想象的那么差,大多数的时候还是能够满足要求的,象微软自己的网页MSDN, www.asp.net论坛也没有去实现静态页面化。看了一下高手的分析,说是动态的网页便于控制权限,新闻的内容生成系统CMS 采用静态页面化确实可以提高系统性能。要不要采用这项技术还是自己考虑吧。
   

asp.net中的实现方法:

查找了很多资料,说我看到的一些实现方法。

1.   url重定向。就是在服务器端采用url重定向,把你对静态页面的请求转到动态页面上去。你访问http://www.widebright.com/254.htm 其实是转到http://www.widebright.com/1.asp?id=254去了。这个其实是假的静态化页面,他根本就没有静态页面的存在。
      对URL重写转向的支持,在Apache服务器上由一非缺省模块(mod_rewrite)来完成,而在IIS下也同样有类似的模块,分别是 ISAPI    REWRITE及IIS    REWRITE。无论是在Apache下还是在IIS下,重写转向的语法都基于正则表达式,只有少量的不同。这个IIS重定向ISAPI是要额外安装扩展才有的,有需要的可以自己在网上找一下。
     在Asp.net下面也可以通过实现http handler 或者http module 模块来实现,象有一个.net 平台的Ajax组件 也是通过实现http handler 来整合的,关于http handler的实现在我的asp.net文件上传组件那篇文章里也有提及,感兴趣的自己学习吧。个人觉得是可以做到url静态请求转换到动态请求页面的这种url映射功能的。
     不过这种方法是要稍稍影响性能的,毕竟多了个ISAPI的环节。

2、采用xml文件,结合xlst文件。这种方法很多人都知道,因为CSDN论坛就是这么做的。我个人对xml方面的技术不太了解也不感兴趣。想了解这方面资料的自己找去吧,反正是通过xlst文件(模版)自动显示xml文件的内容出来。

3.采用Ajax技术,采用这个当然可以做到静态页面了。

4.采用html模版替换技术。就是预先写个html模版文件,然后在生成静态页面时进行替换。这种方法实现起来很容易,也是我想到的方法。网上有很多例子,我就不详细说了。比如写象下面一样一个模版:
<html>




title

content

</html>
  

   生成静态页面的时候用个 string.replace方法把 title   content 等替换成你自己的具体的内容就行了。静态页面的生成可以选择在有数据更新的时候,比如一张帖子又有人回复了,就可以重新生成这张帖子的html文件。当然你这个html模版可以做的更复杂一些了,比如加入css等等啦。

 


静态页面评论功能的实现:


这也是一开始不理解的地方,看网易新闻,百度知道,都是html静态页面都还可以回复,发表评论等。这些评论、回复是要保存到数据库里面去的,不是动态页面才能实现的功能吗?我一开始感到很奇怪。综合众人的说法,网页点击技术等可以通过在 include 页面外javascript脚本的时候指定src 为一个asp之类的动态网页,在那个动态页面实现计数等功能。评论功能的实现有人说在页面下端加一个iframe来显示。在iframe里加一个动态页面确实可以做到的,不过我觉得好像和直接采用动态页面没多大区别。
     还是看看别人是怎么做的吧。我随便打开了个百度知道的页面,查看了一下页面的源代码:


<form method="post" name="fdf" action="http://zhidaocommit.baidu.com/q" onSubmit="return checkcoAndsn(this,'回答');">
<input type="hidden" name="ct" value="22">
<input type="hidden" name="cm" value="100009">
<input type="hidden" name="tn" value="ikreplysubmit">
<input type="hidden" name="qid" value="19229897">
<input type="hidden" name="cid" value="93">
<script language="javascript">
document.write("<input type=hidden name=lu value="+escape(location.href)+ ">");
</script>
<div class="b4g">
<table width="90%" border="0" cellpadding="0" cellspacing="0">
   <tr valign="top">
     <td width="22%" nowrap class="f14"><a name="reply"></a>我也来回答:</td>
     <td width="78%"><textarea name="co" cols="50" rows="10" onKeyDown ="ctlSubmit(event);"   style="width:97%;"></textarea> <br> 回答即可得2分,回答被采纳则获得悬赏分以及奖励20分。 <a href="http://www.baidu.com/search/zhidao_help.html#n4">积分规则</a><br>回答字数在10000字以内</td>
          </tr>
        </table>
        <br> <table width="90%" border="0" cellpadding="0" cellspacing="0">
          <tr valign="top">
            <td width="22%" nowrap class="f14">参考资料:</td>
            <td width="78%"><input name="sn" type="text" size="50"   onKeyDown="ctlSubmit(event);">
              <br> <span class="style3">如果您的回答是从其他地方引用,请表明出处。</span> </td>
          </tr>
        </table>
        <br> <table width="90%" border="0" cellpadding="0" cellspacing="0" >
          <tr>
            <td width="22%" class="f14">&nbsp;</td>
            <td width="78%"><input type="submit"   value="提交回答" class="bnsrh">
              </td>
          </tr>
        </table>
</div>
</form>


可以看到其中的回复的一段如上面所示, 这个段是采用一个form标签框起来的。 好象有几个地方和特殊:比如 form的action属性   ,可能是我以前没做接触网页设计原因吧。我在网上查了一下这个属性,原来表示回复接受数据的网页的。这样通过点击<input type= "submit"   value="提交回答" class="bnsrh"> 这个按钮提交form页面的时候,这个form的数据就发送到 action指定的地址去,由action指定的地址处理数据。明显可以再这里指定个动态网页然后再那个页面上进行处理,完成品评论/回复的提交功能。至于 form标签中的那个method属性却是表示数据提交的方式。method为post的时候,数据是通过http来传送,数据传过去也可以在 request.form数据这里得到,如果method为get的时候,数据是编码后提交到url请求那里,在地址栏中显示出来,在接受页面可以通过 request.params得到这些数据。再查看了163网页的新闻评论那里,就更加清楚了,他那里action指向的就是一个jsp文件,静态页面提交的评论数据都是在jsp动态页面上处理的了。


<form name="formpost" method="post" target="_self" action="http://comment.news.163.com/reply/dopost.jsp" onSubmit="return reply_checksubmit();">
<input name="board" value="" type="hidden">
<input name="threadid" value="" type="hidden">
<input name="url" value="" type="hidden">
<div class="content">
<div class="textArea">
<textarea onfocus="reply_clearfield()" name="body" rows=""></textarea>
</div>
<div class="text">
<a href="http://comment.news.163.com/reply/footprint.jsp">我的灌水记录</a><br />
<input name="hidename" type="checkbox" value="true" checked/>匿名发表 <br /><br /><input name="frmsubmit" type="submit" value=" 提 交" class="s1" />


还可以看到的是百度知道中还有这样的代码:
<input type="hidden" name="ct" value="22">
<input type="hidden" name="cm" value="100009">
<input type="hidden" name="tn" value="ikreplysubmit">
<input type="hidden" name="qid" value="19229897">
<input type="hidden" name="cid" value="93">

这些数据可能是在生成静态页面的时候插进去的,这些hidden 控件的value可以用来在接受回复的时候区分帖子ID等,比如19229897这个就是因为他对应的静态页面是19229897.html。

我还发现一个现象那就是163新闻评论的页面,其实也只是第一个页面是静态的而已,如果一个评论有多页的话,后面的页面也都是jsp动态页面了。这也是应该注意的一个地方吧!静态页面化应用在象新闻啊这些一发布上去就不会变化的页面比较好一点。如果你的内容是不断更新很快的,你也采用这种技术,那么就要不断的重写静态html页面,那样IO操作对服务器性能影响也就很大。所以象索引页这样更新频繁的页面是不能实现静态化的,象百度知道的主页,如果采用静态页面化的话,那么没当有个人发一个提问,就要重写一下html页面,可能就回很慢了。高手的说法是采用缓存来提高性能,象CSDN开发人员说还说什么双缓存技术啊等等。缓冲页面、数据应该在提高性能方面起很大作用的吧,有时间要学一学。   好了不说了,说得越多越显得我没有水平^_^

   写了个实现,验证一下form 的action属性的功能是否象我想的一样可以做静态页面的评论功能的实现的(其实我之前又再找了下资料,确实是这样子用的,有人也说到了做法,是我自己太差了,连这么一个普通的属性都不懂)。

----------------index.htm页面 代码-------------------------
<html>
<head>


</head>

<body>

<form id="form1" action="http://localhost/dddddd/webform1.aspx" method="get"   >
<input type="hidden" name="ct" value="22">
<input type="hidden" name="cm" value="100009">
<input type="hidden" name="tn" value="ikreplysubmit">
<input type="hidden" name="qid" value="19229897">
<input type="hidden" name="cid" value="93">
<textarea name="content" cols="50" rows="10"> </textarea>

<br>
<input type="submit" value="提交"   >

</form>

</body>
</html>
----------------------------------------------------

--------动态接受页面代码WebForm1.aspx----------------------------

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="dddddd.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
   <title>WebForm1</title>
   <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
   <meta name="CODE_LANGUAGE" Content="C#">
   <meta name="vs_defaultClientScript" content="JavaScript">
   <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
   <form id="Form1" method="post" runat="server">
    <FONT face="宋体"></FONT>
   </form>
</body>
</HTML>

--------------------------------------------------------


-------------动态接受页面代码WebForm1.aspx.cs----------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace dddddd
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
   private void Page_Load(object sender, System.EventArgs e)
   {
    // Put user code to initialize the page here

         string content    Page.Request.Form["content"];

    if (content != string.Empty)
    
      Page.Response.Write ("content=");
                  Page.Response.Write (content);

              

    }
    else
    {
      Page.Response.Write ("出现错误");
    }


   }

   #region Web Form Designer generated code
   override protected void OnInit(EventArgs e)
   {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
   }
 
   /// <summary>
   /// Required method for Designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   private void InitializeComponent()
    
    this.Load += new System.EventHandler(this.Page_Load);

   }
   #endregion
}
}


---------------------------------------------------------


动态页面也就是接受到静态页面的数据之后直接输入而已,实际应用中,可能要作些数据库保存记录,然后根据模板重写 html文件更新静态页面,然后还可以重定向到这个页面上去。
需要注意的是,如果在index.htm中form method指定为get的话,那么在动态网页这边只能使用
Page.Request.Params["content"]来获取数据了, 这时 Page.Request.Form["content"]等不到数据了,不过如果method指定为post的话,应为传上来就是form变量,所以
Page.Request.Params["content"]和Page.Request.Form["content"]都可以得到数据。不明白就查一下 method还有Request.Params、Page.Request.Form的区别了。


不过我做的时候还发现上面的代码出现回复提交中文的时候出现乱码的情况。明显是编码问题,在<head> 标签之间加上< meta http-equiv="Content-Type" content="text/html; charset=utf-8">一句指定网页编码。变成下面这样,在提交中文就不会乱码了。

------------------------index.htm-------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body>

<form id="form1" action="http://localhost/d/webform1.aspx" method="post"   >
<input type="hidden" name="ct" value="22">
<input type="hidden" name="cm" value="100009">
<input type="hidden" name="tn" value="ikreplysubmit">
<input type="hidden" name="qid" value="19229897">
<input type="hidden" name="cid" value="93">
<textarea name="content" cols="50" rows="10"> </textarea>

<br>
<input type="submit" value="提交"   >

</form>

</body>
</html>

----------------------------------------------------------

------------WebForm1.aspx----------------------------------------------
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="dddddd.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
   <title>WebForm1</title>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
   <meta name="CODE_LANGUAGE" Content="C#">
   <meta name="vs_defaultClientScript" content="JavaScript">
   <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
   <form id="Form1" method="post" runat="server">
    <FONT face="宋体"></FONT>
   </form>
</body>
</HTML>

 

posted @ 2009-04-02 09:14  MyCoolDog  阅读(3038)  评论(3编辑  收藏  举报