当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET页面静态化 之 (伪静态)

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

2019年05月10日  | 移动技术网IT编程  | 我要评论

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
序号 列名 数据类型 长度 小数位 标识 主键 外键 允许空 默认值 说明
1 id int 4 0 主键,自增
2 parentid int 4 0 父级类别id
3 name nvarchar 50 0 类别名称
表名:t_filterwords
序号 列名 数据类型 长度 小数位 标识 主键 外键 允许空 默认值 说明
1 id int 4 0 主键,自增
2 wordpattern nvarchar 50 0 过滤词
3 replaceword nvarchar 50 0 替换词
表名:t_new
序号 列名 数据类型 长度 小数位 标识 主键 外键 允许空 默认值 说明
1 id int 4 0 主键,自增
2 title nvarchar 50 0 新闻标题
3 body nvarchar 0 0 新闻内容
4 chahhelid int 4 0 类别id
5 ntime datetime 8 3 发布时间
表名:t_review
序号 列名 数据类型 长度 小数位 标识 主键 外键 允许空 默认值 说明
1 id int 4 0 主键自增
2 body nvarchar 0 0 评论内容
3 nid int 4 0 新闻id
4 isvisable bit 1 0 是否显示

(4)创建项目 \
common:全局公用类库文件; wordfilter:显示层代码库; wz喎?/kf/ware/vc/" target="_blank" class="keylink">vcmrgawx0zxiuqkxmo7pctey0+slrsuo/4jxicj4kcgpxb3jkrmlsdgvylkrbtko6refmtprc68dgv+i8yni+cgokv29yzezpbhrlci5nb2rlbdpnb2rlblt6wuva4l/icqoonaoptls9qndczsxb0lhto7okicagicagica8yxnwoljlcgvhdgvyielepq=="news" runat="server">

  • <%#databinder.eval(container.dataitem, "title")%>




  • news.datasource = new bll.t_new().getalllist().tables[0];
    news.databind();
    (6)创建新闻页
        <script src="/js/xheditor/xheditor-1.1.14-zh-cn.min.js" type="text/javascript"></script>
        <script src="/js/xheditor/xheditor_plugins/ubb.min.js" type="text/javascript"></script>
        <script src="/js/xheditor/ubb2html.js" type="text/javascript"></script>
        <script type="text/javascript">
            var url = window.location.host;
            function submitform() {
                var nid = $("#nid").val();
                var body = $("#pingnew").val();
                $.post("https://" + url + "/front1/ashx/postcommon.ashx",
                            { nid: nid, body: body },
                           function (data) {
                               if (data == "ok") {
                                   var valueli = body;
                                   valueli = "" + ubb2html(valueli) + "";
                                   $("#pp > ul").append(valueli);
                                   $("#pingnew").val("");
                               }
                               else if (data == "mod") {
                                   alert("含有审核词,等待管理员审核");
                               }
                               else if (data == "banned") {
                                   alert("含有禁用词,请认真填写评论,文明用语");
                               }
                               else {
                                   alert("错误");
                               }
                           });
            }
    
    
            $(document).ready(function () {
                var nid = $("#nid").val();
                $("#btnping").click(submitform);
    
                $.getjson("https://" + url + "/front1/ashx/getnews.ashx?nid=" + nid,
                function (data) {
                    $.each(data, function (i, item) {
                        var valueli =  item.body ;
                        valueli = "" + ubb2html(valueli) + "";
                        $("#pp > ul").append(valueli);
                    });
                });
    
                var plugins = {
                    code: { c: 'btncode', t: '插入代码', h: 1, e: function () {
                        var _this = this;
                        var htmlcode = '

    html/xmljavascriptcssphpjavapythonperlrubyc#c++/cvb/asp其它

    '; var jcode = $(htmlcode), jtype = $('#xhecodetype', jcode), jvalue = $('#xhecodevalue', jcode), jsave = $('#xhesave', jcode); jsave.click(function () { _this.loadbookmark(); _this.pastetext('[code=' + jtype.val() + ']\r\n' + jvalue.val() + '\r\n[/code]'); _this.hidepanel(); return false; }); _this.savebookmark(); _this.showdialog(jcode); } }, flv: { c: 'btnflv', t: '插入flv视频', h: 1, e: function () { var _this = this; var htmlflv = '

    flv文件: 

    宽度高度: x

    '; var jflv = $(htmlflv), jurl = $('#xheflvurl', jflv), jwidth = $('#xheflvwidth', jflv), jheight = $('#xheflvheight', jflv), jsave = $('#xhesave', jflv); jsave.click(function () { _this.loadbookmark(); _this.pastetext('[flv=' + jwidth.val() + ',' + jheight.val() + ']' + jurl.val() + '[/flv]'); _this.hidepanel(); return false; }); _this.savebookmark(); _this.showdialog(jflv); } } }, emots = { msn: { name: 'msn', count: 40, width: 22, height: 22, line: 8 }, pidgin: { name: 'pidgin', width: 22, height: 25, line: 8, list: { smile: '微笑', cute: '可爱', wink: '眨眼', laugh: '大笑', victory: '胜利', sad: '伤心', cry: '哭泣', angry: '生气', shout: '大骂', curse: '诅咒', devil: '魔鬼', blush: '害羞', tongue: '吐舌头', envy: '羡慕', cool: '耍酷', kiss: '吻', shocked: '惊讶', sweat: '汗', sick: '生病', bye: '再见', tired: '累', sleepy: '睡了', question: '疑问', rose: '玫瑰', gift: '礼物', coffee: '咖啡', music: '音乐', soccer: '足球', good: '赞同', bad: '反对', love: '心', brokenheart: '伤心'} }, ipb: { name: 'ipb', width: 20, height: 25, line: 8, list: { smile: '微笑', joyful: '开心', laugh: '笑', biglaugh: '大笑', w00t: '欢呼', wub: '欢喜', depres: '沮丧', sad: '悲伤', cry: '哭泣', angry: '生气', devil: '魔鬼', blush: '脸红', kiss: '吻', surprised: '惊讶', wondering: '疑惑', unsure: '不确定', tongue: '吐舌头', cool: '耍酷', blink: '眨眼', whistling: '吹口哨', glare: '轻视', pinch: '捏', sideways: '侧身', sleep: '睡了', sick: '生病', ninja: '忍者', bandit: '强盗', police: '警察', angel: '天使', magician: '魔法师', alien: '外星人', heart: '心动'} } }; $('#pingnew').xheditor({ plugins: plugins, tools: 'full', showblocktag: false, forceptag: false, beforesetsource: ubb2html, beforegetsource: html2ubb, emots: emots, emotmark: true, shortcuts: { 'ctrl+enter': submitform} }); }); </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);
    			}
    		}


    如对本文有疑问, 点击进行留言回复!!

    相关文章:

    验证码:
    移动技术网