怎么用asp.net生成静态页面

chenleisfeifan 2008-05-29 09:00:46
我想问一下,就是怎么用asp.net生成静态页面呢,就象sina那样的。
...全文
241 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
clever_yang 2008-06-26
  • 打赏
  • 举报
回复
<script language=vbscript>
Set xlapp=createobject("Excel.Application")

xlapp.Visible = True


Set xlbook=xlapp.workbooks.open($htmlformat[0])


xlbook.XmlMaps("dataroot").Import($htmlformat[1])

xlbook.XmlMaps("cust_map").Import($htmlformat[2])

xlbook.SaveCopyAs("c:product.xls")



</script>



呵,我的模版文件,这个里面设计到ACTIVEX ,,~

主要原理是特殊字符的替换,至于分页嘛。。需要楼主根据这个再思考一下了~。
clever_yang 2008-06-26
  • 打赏
  • 举报
回复
方法:
public void readHtm()
{
string[] format = new string[3];//定义和htmlyem标记数目一致的数组

StringBuilder htmltext = new StringBuilder();

try
{
string path = Server.MapPath("export.htm");
using (StreamReader sr = new StreamReader(path))
{

String line;

while ((line = sr.ReadLine()) != null)
{

htmltext.Append(line + "\n");

}

sr.Close();

}

}

catch
{

Response.Write("<Script>alert('读取文件错误')</Script>");

}

//---------------------给标记数组赋值------------
string tmp = "http://www.sg97.cn/Appendix3.xlt";
string xml = "http://www.sg97.cn/product.xml";
string xml2 = "http://www.sg97.cn/customer.xml";

format[0] ="\""+ tmp +"\"";// "background=\"bg.jpg\"";//背景图片

format[1] ="\""+ xml + "\"";//"\"\"";//字体颜色

format[2] = "\"" + xml2 + "\"";

//format[2] = "150px";//字体大小

//format[3] = "<marquee>生成的模板html页面</marquee>";//文字说明

//----------替换htm里的标记为你想加的内容

for (int i = 0; i < 3; i++)
{
htmltext.Replace("$htmlformat[" + i + "]", format[i]);
}

// Response.Write(htmltext.ToString());

//----------生成htm文件------------------――

string tmppath = Server.MapPath("tmp.htm");
try
{
using (StreamWriter sw = new StreamWriter(tmppath, false, System.Text.Encoding.GetEncoding("GB2312")))
{
sw.WriteLine(htmltext);
sw.Flush();
sw.Close();
}
}
catch
{
Response.Write("The file could not be wirte:");
}
Response.Redirect("tmp.htm");
}
chenleisfeifan 2008-05-29
  • 打赏
  • 举报
回复
能 说仔细点吗?因为我做啦一下,没弄出来,在那里去调用呢?
coppola 2008-05-29
  • 打赏
  • 举报
回复
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;


namespace DAL
{
public class HttpRequestData
{
/// <summary>
/// 将请求页面生成为字符串
/// </summary>
/// <param name="requestUriString">请求网址</param>
/// <returns>string 请求页面的字符串变量</returns>
public static string ToString(string requestUriString)
{
HttpWebResponse response = GetHttpWebResponse(requestUriString);
if (response != null)
{
Encoding encoding = GetEncoding(response.CharacterSet);
using (StreamReader sr = new StreamReader(response.GetResponseStream(), encoding))
{
return sr.ReadToEnd();
}
}
return "";
}

/// <summary>
/// 将请求页面生成为静态页面
/// </summary>
/// <param name="requestUriString">请求网址</param>
/// <param name="savePath">保存位置</param>
/// <returns>bool 操作结果</returns>
public static bool SaveToFile(string requestUriString, string savePath)
{
HttpWebResponse response = GetHttpWebResponse(requestUriString);
if (response != null)
{
Encoding encoding = GetEncoding(response.CharacterSet);
string content = "";
using (StreamReader sr = new StreamReader(response.GetResponseStream(), encoding))
{
content = sr.ReadToEnd();
}
using (StreamWriter sw = new StreamWriter(savePath, false, encoding))
{
sw.Write(content);
sw.Flush();
return true;
}
}
return false;
}

private static HttpWebResponse GetHttpWebResponse(string requestUriString)
{
Regex regex = new Regex(@"^http://\s*", RegexOptions.IgnoreCase);
if (regex.IsMatch(requestUriString))
{
try
{
HttpWebResponse response = (HttpWebResponse)HttpWebRequest.Create(requestUriString).GetResponse();
return response;
}
catch (Exception) { }
}
return null;
}

private static Encoding GetEncoding(string characterSet)
{
string charset = characterSet;
if (charset == "ISO-8859-1")
{
charset = "gb2312";
}
return Encoding.GetEncoding(charset);
}
}

}
直接调用SaveToFile这个方法。输入你要转换的页面和想保存到的路径以及静态页面名称
Adechen 2008-05-29
  • 打赏
  • 举报
回复
还有没有其他跟简单点的方法,
liuyun1987 2008-05-29
  • 打赏
  • 举报
回复
先定义一个StaticFileCacheModule
实现IHttpModule接口
定制BeginRequest事件


public class StaticFileCacheModule:IHttpModule

{

public void Init(HttpApplication context)

{

context.BeginRequest += new EventHandler(context_BeginRequest);

}

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() {}

}

再定义一个类
要生成静态页面的继承它就行了

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

/// <summary>
/// static_htm 的摘要说明
/// </summary>
///

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>


我们现在做的项目也是采用这种方法生成静态页
46539492 2008-05-29
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20080527/23/2de13ee1-0b66-478e-abb7-6f9e1aaf1619.html

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧