地球人都知道,静态HTML页面通常会比服务器端页面如asp、aspx页面要来的快,即使这些页面没有服务器端代码。
另外要命的是,这些页面在主流的搜索引擎能中最为吃香,和那些aspx还带几个尾巴参数的页面比起来,真是天上地下。
如果那天老板发现这个问题,叫你把辛辛苦苦实现的服务器端程序向静态HTML页面靠拢,你会做何感想?
有一种URL重写的方案可以实现对搜索引擎的欺骗,除了这种方法,自动生成静态HTML页面应该是最彻底的方法了。
言归正传,开始介绍如何实现吧
1. 引用Nvelocity0.5,记得是0.5哦,NVelocity0.4我试过好久,好像不行,好像和路径有关系。
2、引用一些需要的命名空间


using
NVelocity;

using
NVelocity.App;

using
NVelocity.Exception;

using
NVelocity.Runtime;

using
NVelocityTemplateEngine;

using
NVelocityTemplateEngine.Interfaces;

3、初始化一些变量来使用


INVelocityEngine fileEngine;

IDictionary context;






///

<summary>


///
初始化NVelocity模板引擎并加载程序的配置信息e

///

</summary>



protected

void
InitTemplateEngine()



{

context

=

new
Hashtable();

string
templateDirectory
=
Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
"
Articles
"
);

fileEngine

=
NVelocityEngineFactory.CreateNVelocityFileEngine(templateDirectory,
true
);

}


4、页面生成代码



public

override

void
Execute()



{

string
message
=

string
.Format(
"
Create the Helper class file.
"
);

log.Debug(message);


string
sql
=

string
.Format(
"
select * from article
"
);

if
(
!
isCreateAll)



{

sql

=

string
.Format(
"
select * from article where generated =False
"
);

}




using
(IDataReader reader
=
xConfig.ExecuteReader(sql))



{

while
(reader.Read())



{

PrepareClass(reader);

OutputFile();

}



}




sql

=

"
update article set generated =True
"
;

if
(
!
isCreateAll)



{

sql

=

"
update article set generated =True  where generated =False
"
;

}



xConfig.ExecuteNonQuery(sql);

}






///

<summary>


///
Prepares the class content.

///

</summary>



private

void
PrepareClass(IDataReader reader)



{

FileNameOfOutput

=

string
.Format(
"
{0}#{1}
"
, ((DateTime) reader[
"
datetime
"
]).ToString(
"
yyyy-MM-dd
"
), reader[
"
id
"
].ToString());


context.Clear();

context.Add(

"
id
"
, reader[
"
id
"
].ToString());

context.Add(

"
category
"
, reader[
"
category
"
].ToString());

context.Add(

"
title
"
, reader[
"
title
"
].ToString());

context.Add(

"
content
"
, reader[
"
content
"
].ToString());

context.Add(

"
datetime
"
, reader[
"
datetime
"
].ToString());

}







///

<summary>


///
根据模板创建输出的文件

///

</summary>



public

virtual

void
OutputFile()



{

if
(fileEngine
!=

null
)



{

string
filePath
=
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, directoryOfOutput);

string
fileName
=
Path.Combine(filePath, fileNameOfOutput
+
fileExtension);


DirectoryInfo dir

=

new
DirectoryInfo(filePath);

if
(
!
dir.Exists)



{

dir.Create();

}




log.Debug(

string
.Format(
"
Class file output path:{0}
"
, fileName));

using
(StreamWriter writer
=

new
StreamWriter(fileName,
false
))



{

fileEngine.Process(context, writer,

this
.templateFile);

}



}



}


5、界面层生成页面



string
template
=

"
page.htm
"
;

try




{

HelperClassAdapter helper

=

new
HelperClassAdapter(template,
false
);

helper.Execute();

Response.Write(

"
<script>alert('生成成功');</script>
"
);

}



catch
(Exception ex)



{

Helper.ShowError(

this
, ex,
false
);

return
;

}



页面生成就可以了,具体做法自己琢磨就可以了。
页面模板文件


<
HTML
>


<
HEAD
>


<
TITLE
>
$title
</
TITLE
>


<
META
http-equiv
=Content-Type
content
="text/html; charset=UTF-8"
>


<
META
content
="$title"
name
=description
>


<
META
content
="$title"
name
=keywords
>


</
HEAD
>


<
BODY
>


<
strong
class
="style3"
>
$title
</
strong
></
h2
>


<
div
>
$content
</
div
>


<
hr
width
="98%"
/>


<
div
align
="right"
>
$datetime
</
div
>


</
BODY
>


</
HTML
>

标签: none

添加新评论