首页 > 代码库 > ASP.NET页面静态化 之 (伪静态)

ASP.NET页面静态化 之 (伪静态)

1.页面为何要静态化

有利于搜索引擎优化(SEO)
使页面URL看起来更正规
真静态 的效率更高,并且更安全,HTML页面不会受Asp.Net相关漏洞的影响;

2.伪静态原理

其原理就是,在一个页面请求的执行期间将伪静态的URL重写成真实的URL路径;

3.那种条件下需要静态化

一个页面经常被访问,并且页面内容基本不变的;
所有用户访问的页面内容基本一样的。
例如:(新闻模块)

4.下面开始带领大家写一个伪静态的案例(新闻模块)

(1)先创建新闻模块数据库

T_Channels表,存储新闻类别
T_FilterWords表,存储评论的过滤词
T_new表,存储新闻信息
T_Review表,存储回复信息
(2)建表SQL语句
USE [WordFilter]
GO
/****** Object:  Table [dbo].[T_Review]    Script Date: 07/20/2014 18:27:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[T_Review](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[body] [nvarchar](max) NULL,
	[nid] [int] NOT NULL,
	[Isvisable] [bit] NULL,
 CONSTRAINT [PK_T_Review] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[T_new]    Script Date: 07/20/2014 18:27:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[T_new](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[title] [nvarchar](50) NULL,
	[body] [nvarchar](max) NULL,
	[chahhelid] [int] NULL,
	[ntime] [datetime] NULL,
 CONSTRAINT [PK_T_new] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[T_FilterWords]    Script Date: 07/20/2014 18:27:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[T_FilterWords](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[WordPattern] [nvarchar](50) NULL,
	[ReplaceWord] [nvarchar](50) NULL,
 CONSTRAINT [PK_T_FilterWords] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[T_Channels]    Script Date: 07/20/2014 18:27:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[T_Channels](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[ParentId] [int] NOT NULL,
	[Name] [nvarchar](50) NULL,
 CONSTRAINT [PK_T_Channels] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

(3)字段解释
数据库名:WordFilter
表名:T_Channels
序号列名数据类型长度小数位标识主键外键允许空默认值说明
1Idint40   主键,自增
2ParentIdint40      父级类别id
3Namenvarchar500      类别名称
表名:T_FilterWords
序号列名数据类型长度小数位标识主键外键允许空默认值说明
1idint40    主键,自增
2WordPatternnvarchar500      过滤词
3ReplaceWordnvarchar500      替换词
表名:T_new
序号列名数据类型长度小数位标识主键外键允许空默认值说明
1idint40    主键,自增
2titlenvarchar500      新闻标题
3bodynvarchar00      新闻内容
4chahhelidint40      类别id
5ntimedatetime83      发布时间
表名:T_Review
序号列名数据类型长度小数位标识主键外键允许空默认值说明
1idint40    主键自增
2bodynvarchar00      评论内容
3nidint40      新闻id
4Isvisablebit10      是否显示

(4)创建项目

Common:全局公用类库文件;
WordFilter:显示层代码库;
WordFilter.BLL:BLL代码层库
WordFilter.DAL:DAL代码类库
WordFilter.Model:Model代码类库
(5)创建新闻列表:
        <asp:Repeater ID="news" runat="server">
            <ItemTemplate>
            <li><a href=http://www.mamicode.com/‘Article-.aspx‘>
            </ItemTemplate>
        </asp:Repeater>

news.DataSource = new BLL.T_new().GetAllList().Tables[0];
news.DataBind();
(6)创建新闻页
    <script src=http://www.mamicode.com/"/js/xheditor/xheditor-1.1.14-zh-cn.min.js" type="text/javascript"></script>>
评论通过ajax提交和获取,有助于下一片 的页面真静态教程
public string title;
		protected string body;
		protected int nid;
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				if (Request.QueryString["newid"] != null)
				{
					int newid =int.Parse(Request.QueryString["newid"].ToString());
					nid = newid;
					BindNews(newid);
				}
			}
		}
		protected void BindNews(int newid)
		{
			Model.T_new news = new BLL.T_new().GetModel(newid);
			if (news != null)
			{
				title = news.title;
				body = news.body;
			}
		}

(6)核心
在Global.asax的Application_BeginRequest处理请求的URL
		protected void Application_BeginRequest(object sender, EventArgs e) {
			//获取请求的URL(虚拟路径)
			string url = HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath;
			//用正则验证是否符合 @"~/Front1/Article-(\d+)\.aspx"
			Match match = Regex.Match(url, @"~/Front1/Article-(\d+)\.aspx");
			//如果符合此验证
			if (match.Success)
			{
				//获取文章的id
				string id = match.Groups[1].Value;
				//控制服务器执行 下面URL
				HttpContext.Current.RewritePath("~/Front1/Article.aspx?newid="+id);
			}
		}