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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 鱼十七

[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">

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