惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
G
Google Developers Blog
H
Help Net Security
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog RSS Feed
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
D
Docker
Hugging Face - Blog
Hugging Face - Blog
I
InfoQ
博客园 - 司徒正美
Last Week in AI
Last Week in AI
Microsoft Security Blog
Microsoft Security Blog
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence

博客园 - 鱼十七

[Programming Entity Framework] 第3章 查询实体数据模型(EDM)(二) [Programming Entity Framework] 第3章 查询实体数据模型(EDM)(一) [Programming Entity Framework] 第2章 探究实体数据模型(EDM)(三) [Programming Entity Framework] 第2章 探究实体数据模型(EDM)(二) [Programming Entity Framework] 第2章 探究实体数据模型(EDM)(一) [Programming Entity Framework] 第1章 ADO.NET实体框架介绍(二) [Programming Entity Framework] 第1章 ADO.NET实体框架介绍(一) Programming Entity Framework 第二版 翻译索引 设计模式学习笔记 1.介绍 WP7 学习手记1.你好 WP7 excel2007内容转成xml scrum介绍 SQL SERVER 2005服务无法启动问题的解决办法 C#操作Excel知识点 [翻译]MS project 与 MS Team Foundation Server(TFS)的域映射 [翻译]通过调用多个动作创建ASP.NET MVC视图 [翻译]在ASP.NET MVC中绑定数据(包括分页和排序) MOSS文章过滤QueryString Filter Web Parts使用及Web Parts部署 ASP.NET方式在工作组内修改帐户密码
ASP.NET 生成静态页面的思路介绍
鱼十七 · 2011-03-22 · via 博客园 - 鱼十七

今天研究静态页面的生成,将学习内容简单记录如下。

目前生成静态页面的方法大致分为两种,一种是直接访问动态页面地址,将其生成的html代码保存成静态页面。另一种是通过读取页面模板,对其中需要替换的内容进行替换的方式进行生成。其中前一种方法简单,对于生成单个页面或少量页面比较实用,而对大量的页面且页面之间彼此关联复杂的,第一种就不太方便。对于使用模板的方法稍微复杂,这里不详细讨论,只给出第一种方法应对不太复杂的项目的应用。

给定生成静态页面入口页面地址,比如Index.aspx,通过查找其中以href=开始的链接的页面地址,对其按一定规则进行替换后,生成静态化之后的Index.html,再依次对Index.aspx中的所有链接页面依次进行静态化,如此循环。

下面的示例代码演示了如何将页面中的动态链接地址替换成按规则命名的静态地址。



using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;namespace WebTest
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string content = "<a target=\"_blank\" href=\"Product.aspx?classId=123\"><a target=\"_blank\" href=\"Product-view.aspx\"><a target=\"_blank\" href=\"Product-view.aspx?id=59\"><a target=\"_blank\" href=\"Product-view.aspx?id=11159\">";
string newContent = content;
Regex rg
= new Regex("href="); //正则定位到链接
int len = 5; //正则字符长度
MatchCollection mc = rg.Matches(content);
foreach (Match m in mc)
{
int startIndex = m.Index + len + 1; //定位到的URL的起始位置
int endIndex = content.IndexOf("\"", m.Index + len + 1); //定位到的URL的结束位置
string originalURL = content.Substring(startIndex, endIndex - startIndex); //获取到URL的全地址
string newURL = "";
newURL
= originalURL.Replace(".aspx?classId=", "-class-"); //产品类型的替换
newURL = newURL.Replace(".aspx?id=", "-"); //产品的替换
newURL = newURL.Replace(".aspx", "");
newURL
+= ".html";

newContent

= newContent.Replace(originalURL +"\"", newURL +"\""); //替换掉原URL地址为静态地址
}

Response.Write(

string.Format("原内容:{0}<br/>新内容:{1}", content.Replace("<", "&lt;").Replace(">", "&gt;"), newContent.Replace("<", "&lt;").Replace(">", "&gt;")));
}
}
}

该页面运行效果结果如下:

原内容:<a target="_blank" href="Product.aspx?classId=123"> <a target="_blank" href="Product-view.aspx"> <a target="_blank" href="Product-view.aspx?id=59"> <a target="_blank" href="Product-view.aspx?id=11159">
新内容:<a target="_blank" href="Product-class-123.html"> <a target="_blank" href="Product-view.html"> <a target="_blank" href="Product-view-59.html"> <a target="_blank" href="Product-view-11159.html">

以上只是一点思路,更多内容有待继续研究学习。