首页 > 代码库 > 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
|
表名:T_FilterWords
|
表名:T_new
|
表名:T_Review
|
(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>
<ItemTemplate>
<li><a href=http://www.mamicode.com/‘Article-.aspx‘>
</ItemTemplate>
</asp:Repeater>
news.DataSource = new BLL.T_new().GetAllList().Tables[0];
news.DataBind();
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); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。