动转静分两种:伪静态和真静态
先说下伪静态,伪静态要做三步工作
第一步:在Web.Config 中加入这些代码:
<urls>
<add virtualUrl="~/default.htm" destinationUrl="~/default.aspx" /> -----没有参数的页面
<add virtualUrl="~/news_(\d+).htm" destinationUrl="~/news.aspx?page=$1" />------- 一个参数
</urls>
</CustomConfiguration>
using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Util;
using System.Web;
namespace wave.gong
{
/// <summary>
/// HtmlFileCreator 的摘要说明。
/// </summary>
public class HtmlFileCreator
{
public StringWriter html;
private HtmlTextWriter htmlWriter;
public string newUrl;
// override the HtmlTextWriter to reach the constructor
// the constructor in the base class is protected
class HtmlFileCreator: HtmlTextWriter
{
public HtmlFileCreator(TextWriter tw) : base(tw) { }
}
// publish the HTMLwriter
public HtmlTextWriter RenderHere
{
get {return htmlWriter;}
}
// constructor initializes stringwriter and htmlwriter based on that
// initialize Url
public HtmlFileCreator()
{
html = new StringWriter();
htmlWriter = new MyHtmlTextWriter(html);
newUrl = System.Web.HttpContext.Current.Request.Url.AbsolutePath.ToString();// Context.Request.Url.AbsolutePath.ToString();
newUrl = newUrl.Replace(".aspx",".html");
}
public void WriteHTMLFile(string virtualFileName)
{
// Stringreader reads output rendered by asp.net
/ / Stringwriter writes html output file
StringReader sr = new StringReader(html.ToString());
StringWriter sw = new StringWriter();
// Read from input
string htmlLine = sr.ReadLine();
while (htmlLine != null)
{
// Filter out ASp.net specific tags
if (!((htmlLine.IndexOf("<body") > 0) ||
(htmlLine.IndexOf("__VIEWSTATE") > 0) ||
(htmlLine.IndexOf("</body>") > 0)))
{sw.WriteLine(htmlLine);}
htmlLine = sr.ReadLine();
}
// Write contents stringwriter to html file
// StreamWriter fs = new StreamWriter(virtualFileName);
StreamWriter fs=new StreamWriter(virtualFileName,false,System.Text.Encoding.UTF8);
fs.Write(sw.ToString());
fs.Close();
}
}
}
写的话 很复杂 而且有可能还看不懂
还是给个例子大家把 源码点这里下载
下载地址:http://www.hi002.com/Wave_Demo.rar
上面是本人原创 转载请说明来源
下面是网络上搜索来的 都是这样的
众所周知,百度,谷歌等大型搜索系统比较喜欢.htm结尾的静态页面。而对于现在大部分企业网站来说实现动态是必须的。这就会影响到百度
,谷歌的收录以及排名。网站的动转静这时显得很重要。网站动转静也成为了绝大多数网站程序员追求。可是怎么实现网站的动静转换呢?首
先要熟记网站的不同语言的读写操作,比如:用asp.net来实现动转静:
(一)读
string temp = HttpContext.Current.Server.MapPath("~/template/");
string m = temp + "index.htm";
System.IO.StreamReader sr = new System.IO.StreamReader(m,System.Text.Encoding.Default,false);
string html = sr.ReadToEnd();
对上的一段代码的理解是:定义一个变量temp用来存放网站模板的文件夹的路径。所有的模板都在根目录下的template里面。变量m是指在
template文件里模板index.htm的路径。sr是System.IO.StreamReader 类型的容器,StreamReader可以简单理解是用来读取文件的类型,用来
存放从m路径读取到的文件代码。读取完毕(sr.ReadToEnd)后,把代码放到变量html里,这时index.htm里所有的代码都已经放到了html里。
(二)取代标签
string title = "我的第一个asp.net动转静";
string content = "我一定要记住这些语句!";
html=html.Replace("{title}", title);
html=html.Replace("{content}", content);
对上面的代码理解是:定义两个变量title和content。然后把“我的第一个asp.net动转静”放到变量title里面;把“我一定要记住这些语句
!”放到变量content里面。这些都很容易理解。可是{title},{content}怎么理解呢?简单的说他就是板里标记。你也可以用[title]和
[content]或者!title和!content来做标记。这就要看个人喜好了。我有一个简单的index.htm的模板。代码如下:
<html>
<head>
<title>
{title}<!--题目标签,本例要替换的部分-->
</title>
</head>
<body>
{content}<!--内容标签,本例要替换的部分-->
</body>
</html>
可是怎么替换呢?我们用到了RePlace()这个函数。比如:
html=html.Replace("{title}", title);//把模板里的{title}标签替换成变量title里的内容。
html=html.Replace("{contentle}",content););//把模板里的{content}替换成变量content里的内容。
(三)写
string path = HttpContext.Current.Server.MapPath("~/index.htm");
System.IO.StreamWriter sw = new System.IO.StreamWriter(path,false,System.Text.Encoding.Default);
sw.Write(html);
对于以上代码的理解是:定义一个变量path,用来存放所要生成页面的路径。比如我要把生成的文件放到要目录下,并且命名为index.htm。那
么string path = HttpContext.Current.Server.MapPath("~/index.htm");
这时要用来到StreamWriter这个对象。这个对象是用来执行“写”操作的。格式如下:
System.IO.StreamWriter sw = new System.IO.StreamWriter(path,false,System.Text.Encoding.Default);
上面sw表示来用执行“写”操作的对象。用这个对象里有个write方法。就是利用这个方法来执行“写”操作。
比如:sw.Write(html);//把上面代码中的html变量的内容写到path路径下,并命名为index.htm。
然后关闭sw对象。比如:sw.Close();
这样一个简单的动静转换就完成了。
asp的动静转换也是如此的思想,只不过用到的代码不一样。但“读”“替换”“写”思想是一样的。
有句话,网站编程不难,难的是编程的思想。
百度搜索的全是这样的 我看不到不知道是什么回事都是什么模板的 别人要自己设计的前台那写怎么写呢?
我发现在网上搜索不到一点实用的动转静的技术 就写了这边文章 希望能给Asp.net编程人员有所帮助 Asp.net初学者有帮助
这一边文章暂时写到这里 在动转静的过程中 还会遇到一些问题 伪静态没什么问题
是在真静态的过程中的 真静态在分页过程中的一些问题 我单独写一篇文章写真静态分页的问题
本文转载自:http://hi.baidu.com/gongjiutao/blog/item/75631068616a00e342169458.html