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

推荐订阅源

量子位
P
Privacy International News Feed
Security Latest
Security Latest
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Last Watchdog
The Last Watchdog
S
Schneier on Security
Engineering at Meta
Engineering at Meta
Google Online Security Blog
Google Online Security Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Cyberwarzone
Cyberwarzone
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
A
Arctic Wolf
博客园 - 聂微东
I
Intezer
腾讯CDC
罗磊的独立博客
T
Tailwind CSS Blog
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
L
Lohrmann on Cybersecurity
人人都是产品经理
人人都是产品经理
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Simon Willison's Weblog
Simon Willison's Weblog
W
WeLiveSecurity
N
News and Events Feed by Topic
SecWiki News
SecWiki News
S
Security Affairs
T
Threat Research - Cisco Blogs
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
T
Troy Hunt's Blog
N
News and Events Feed by Topic
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
酷 壳 – CoolShell
酷 壳 – CoolShell
M
MIT News - Artificial intelligence
I
InfoQ
Hacker News: Ask HN
Hacker News: Ask HN
T
The Blog of Author Tim Ferriss
Schneier on Security
Schneier on Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
Vercel News
Vercel News
博客园 - 三生石上(FineUI控件)
D
Darknet – Hacking Tools, Hacker News & Cyber Security

博客园 - 鱼十七

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

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