目前我只会:
1.生成静态首页的方法.
2.对新闻利用模板生成静态页面的方法.
请教:如何将有gridview和datalist的aspx页面生成静态页面?
1.生成静态首页的方法.
2.对新闻利用模板生成静态页面的方法.
请教:如何将有gridview和datalist的aspx页面生成静态页面?
解决方案 »
- [大家一起来讨论] [OA系统的权限怎么设置好啊 ]
- 项目做昏头了,求一个问题的答案,来挽救前期策略上的失误,关于文件下载的问题
- 怎么用服务端代码关闭一个打开的弹出窗口?
- 遇到困难:上传文件后,如何把相应的文件名放到数据库字段中...
- Object reference not set to an instance of an object为什么会出现这情况?
- 关于Session过期时间
- 怎么在.aspx页面上显示winform控件!
- 使用C#的WEB编程,请问:如何得到DataGrid中的checkbox的值?谢谢!
- .net来用OracleConnection、OracleCommand取数据存在乱码,请各位高手帮忙。急需!!!
- houjianxun请进来,:)
- 配置错误
- .net 非常诡异的事情
//urlstr是静态文件名 如:
//urlstr="index.htm";
protected override void Render(HtmlTextWriter writer)
{
if (!File.Exists(Server.MapPath(urlstr)))
{
System.IO.StringWriter html = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter tw = new System.Web.UI.HtmlTextWriter(html);
base.Render(tw);
System.IO.StreamWriter sw;
sw = new System.IO.StreamWriter(Server.MapPath(urlstr), false, System.Text.Encoding.Default);
sw.Write(html.ToString());
sw.Close();
tw.Close();
//Server.Transfer(urlstr);
Response.Redirect(urlstr);
}
else
{
Response.Redirect(urlstr);
//Server.Transfer(urlstr);
}
}
现在我的内容页里有要做html.
有没有别的办法?
具体做法可以参考:
==============================
先定义一个StaticFileCacheModule
实现IHttpModule接口
定制BeginRequest事件
public class StaticFileCacheModule:IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
} private void context_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context; //判断是否需要处理
if (context.Request.AppRelativeCurrentExecutionFilePath.ToLower().EndsWith(".aspx"))
{ string fileUrl = "~/CacheFile/"; string fileName = GetFileName(context); string filePath = context.Server.MapPath(fileUrl) + fileName; if (File.Exists(filePath)) { //如果静态缓存文件存在,直接返回缓存文件 context.RewritePath(fileUrl + fileName, false); } } } public static string GetFileName(HttpContext context) { //我们的缓存文件名由页面文件名加上查询字符串组成 return context.Request.AppRelativeCurrentExecutionFilePath.ToLower() .Replace(".aspx", "").Replace("~/", "").Split('/')[context.Request.AppRelativeCurrentExecutionFilePath.ToLower() .Replace(".aspx", "").Replace("~/", "").Split('/').Length-1] + context.Request.Url.Query.Replace("?", "__").Replace("&", "_") + ".html"; } public void Dispose() {}}再定义一个类
要生成静态页面的继承它就行了 public abstract class static_htm : System.Web.UI.Page
{
protected override void Render(HtmlTextWriter writer) { StringWriter sw = new StringWriter(); HtmlTextWriter htmlw = new HtmlTextWriter(sw); //调用Render方法,把页面内容输出到StringWriter中 base.Render(htmlw); htmlw.Flush(); htmlw.Close(); //获得页面内容 string pageContent = sw.ToString(); string path = Server.MapPath("~/CacheFile/"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string pageUrl = StaticFileCacheModule.GetFileName(HttpContext.Current); if(!File.Exists(pageUrl)) { //把页面内容保存到静态文件中 using (StreamWriter stringWriter = File.CreateText(path + pageUrl)) { stringWriter.Write(pageContent); ; } }
//将页面内容输出到浏览器 Response.Write(pageContent); }
}当然不要忘在配置web.confing
<httpModules> <add name ="StaticFileCache" type="StaticFileCacheModule"/> </httpModules>
StaticFileCacheModule是一个类文件??
是要把分页的全部页面生成静态吗?
datalist页面生成的html代码可是点下一页的话要访问的还是aspx页面..而且文件名也要和生成的html页面名称相同.