为了利于SEO优化,最近在项目中需要把某些页面转化为静态页面,现把相关思路整理如下。
一,先建立一个模版页
为了适应实际应用功能,一个网站往往需要最终用户自定义静态化的模版,本文对这方面不作过多描述,主要只是增删改查功能,此章仅作抛砖引玉用!!!前端界面如下
aspx.cs页面代码也只简单实现对模版的保存
/// <summary>
/// Saves the data.
/// </summary>
/// <param name="id">The id.</param>
private void SaveData(int id)
{
Model.C_Template model = GetData(id);
if (model != null)
{
long x = 0;
if (id <= 0)
{
x = model.id = _BLL.Add(model);
}
else
{
x = _BLL.Update(model) ? 1 : 0;
}
//other code
//show message
if (x > 0)
{
MessageBox.Show(string.Format(_jsonResult, "true", "保存成功", _returnUrl, id.ToString()), MessageEnum.Error);
}
else
{
MessageBox.Show(string.Format(_jsonResult, "false", "保存失败,请稍候再试...", "", id.ToString()), MessageEnum.Error);
}
}
else
{
//有错误
MessageBox.Show(string.Format(_jsonResult, "false", _pageMsg, "", id.ToString()), MessageEnum.Error);
}
}
二,根据对应模版,静态出一个include页面
ok,好吧,其实我这里取巧了,先跳过,后面总结里再详说。为了使本例简单,现在先假设模版页已经生成了html格式。代码如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{$title} - {$classcName} - <!--#include file="/include/common/siteTitle.html" --></title>
<meta name="description" content="{$seoDescription},<!--#include file="/include/common/siteDescription.html" -->" />
<meta name="keywords" content="{$seoKeyWords},<!--#include file="/include/common/siteKeyWords.html" -->" />
<link href="/css/layout.css" rel="stylesheet" type="text/css" />
<link href="/css/layout_sub.css" rel="stylesheet" type="text/css" />
<script src="/js/jquery/jquery.js" type="text/javascript"></script>
<script src="/js/jquery/myJS.js" type="text/javascript"></script>
<!--[if lte IE 6]>
<script type="text/javascript" src="/js/ie6png.js"></script>
<![endif]-->
<script type="text/javascript">
$(function () {
$(".hotSchool .tabs").tabs(".hotSchool div.contentC", { event: 'click', effect: 'fade' });
});
</script>
</head>
<body>
<div id="bgImgWrap">
<img src="/images/back1.jpg" alt="" />
</div>
<!--#include file="/include/common/picStory.html" -->
<div class="bigWrap">
<div class="sideWrapBg png"></div>
<div class="sideWrap png">
<div class="logo noTxt clearfix"></div>
<!--#include file="/include/common/sideNav.html" -->
</div>
<div class="mainWrap png">
<!-- top search /-->
<!--#include file="/include/common/head.html" -->
<!-- 频道页顶部导航 /-->
<div class="cWrap subTop clearfix">
<div class="left subTitle">{$channelname}</div>
<div class="left subNav">
<!--{$subNav}-->
</div>
</div>
<!-- sub wrap -->
<div class="cWrap subWrap pie png clearfix">
<!-- 文章详细 /-->
<div class="detailWrap newsListWrap newsDetailWrap clearfix">
<div class="subNavWrap clearfix">
<div class="subNav left">{$locationget}</div>
</div>
<h1>{$title}</h1>
<div class="infoWrap clearfix">
<div class="shareWrap">
<div id="bdshare" class="bdshare_t bds_tools get-codes-bdshare">
<span class="left">分享本页:</span>
<a class="bds_qzone"></a>
<a class="bds_tsina"></a>
<a class="bds_tqq"></a>
<a class="bds_renren"></a>
<a class="bds_douban"></a>
<span class="bds_more"></span>
</div>
</div>
<div class="dateWrap">发表:{$addDate} </div>
<div class="readWrap">阅读:<span id="viewNums">...</span></div>
</div>
<div class="clearfix"></div>
<div class="subHR clearfix png"></div>
<!-- Intro /-->
<div class="introWrap clearfix">
{$intro}
</div>
<!-- 正文 /-->
<div class="txtWrap clearfix minArticle">
{$content}
</div>
<!-- 相关文章 /-->
<div>
</div>
</div>
</div>
<!--/ sub wrap -->
<!-- footer /-->
<!--#include file="/include/common/foot.html" -->
</div>
</div>
</body>
</html>
ok,现在我们把这个页面命名为“CS1.html”,放在根目录下的“Template”文件夹下,那么,我们建一个“include.aspx”页面,作用户输入文章用,那么
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="include.aspx.cs" validateRequest="false" Inherits="Admin_CMS_include" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>文章输入页面</title>
</head>
<body>
<form id="form1" runat="server">
<div>
文章标题:<asp:TextBox ID="txtTitle" runat="server" Height="26px" Width="350px"></asp:TextBox><br />
文章摘要:<asp:TextBox ID="txtIntro" runat="server" Height="26px" Width="350px"></asp:TextBox><br />
文章内容:<asp:TextBox ID="txtContent" runat="server" Height="179px" TextMode="MultiLine" Width="350px"></asp:TextBox><br />
<br />
<asp:Button ID="btnInclude" runat="server" OnClick="btnInclude_Click" Text="生成静态页" />
</div>
</form>
</body>
</html>
aspx.cs页面代码如下,记得引用
using System.IO;
using System.Text;
那么,主要代码如下:
protected void btnInclude_Click(object sender, EventArgs e)
{
//other code;
//Get date into dataSource;
string mbPath = Server.MapPath("~/Web/Template/CS1.html");
Encoding code = Encoding.GetEncoding("UTF-8");
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
try
{
sr = new StreamReader(mbPath, code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
string fileName = DateTime.Now.ToString("MMddHHmm") + ".html";//保存静态页面的扩展名
//整章的关键点,替换掉相应字符串
str = str.Replace("{$Title}", txtTitle.Text);
str = str.Replace("{$intro}", txtIntro.Text);
str = str.Replace("{$content}", txtContent.Text);
//生成静态文件
try
{
sw = new StreamWriter(Server.MapPath("~/html/") + fileName, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
Response.Write("<a href=/Web/html/" + fileName + " mce_href=" + fileName + " target=_blank>" + fileName + "</a>已经生成!");
}
}
三,总结
ok,原理已经明白了。那么,从效益出发,实际应用中应该是怎样的呢?一:每先建一个静态页面模版,就相应生成一个静态模版页;
二:或者直接从数据库中读取模版的数据流,然后相应替换;
三:确定保存文章的时候,立刻生成一个相应的静态文章页;
四:在网站后台管理中,嵌入“一键生成静态页面”中,重新检索生成漏掉或者有更改的静态页面。
那么,其它比如是否细分类别来实现对应的静态化功能,就以自己实际项目来考虑了最后,附最后的效果图。。。。