日期:2014-05-18  浏览次数:20373 次

.net(2.0) 生成 HTML
问个问题,.Net(2.0)如何生成静态的HTML页面
最好是数据的显示都在一张静态的页面上显示.要不一条信息生成一个页面不太现实
希望能有详细些的代码,感激不尽

------解决方案--------------------
<%@ Page validateRequest="false" %>
<%@ Import NameSpace="System.Xml"%>
<%@ Import NameSpace="System.IO"%>
<script language="C#" runat="server">
void Page_Load(Object sender,EventArgs e)
{

}

void genFile(Object sender,EventArgs e)
{

bool IsDebug = true;
//1,先检查目录,目录规则是:yyyy-mm的格式;

string path = Request.PhysicalApplicationPath;
if (IsDebug)
{
Response.Write ("<li>" + Request.PhysicalApplicationPath);
Response.Write ("<li>" + Request.ApplicationPath);
}

//如果不是在应用程序根目录下,需要的操作:
// path = path + "allnews" + Path.DirectorySeparatorChar;

//2,定义日期目录
string dt = DateTime.Now.ToString("yyyy-MM");
path = path + dt + Path.DirectorySeparatorChar;
if (IsDebug)
{
Response.Write ("<li>dt = " + dt);
Response.Write ("<li>path = " + path);
}
//3,判断目录

if(!Directory.Exists(path))
Directory.CreateDirectory(path);

//4,组合文件名字的物理路径:格式c:\xxx\xxx和url路径,格式:/xxx/xx.htm
//FileNameUrl用来写数据库

string FileName = System.Guid.NewGuid().ToString("D") + ".htm";
string FileNameUrl = Request.ApplicationPath + dt + "/" + FileName;

if (IsDebug)
{
Response.Write ("<li>FileName = " + FileName);
Response.Write ("<li>FileNameUrl = " + FileNameUrl);
}

//5,开始写文件

string content = TextContent.Text;//这里用这个作例子,可以改成Request.Form
//文件名字可以按你的规则修改

FileStream fs = new FileStream(path + FileName, FileMode.CreateNew, FileAccess.Write, FileShare.None);
StreamWriter sr = new StreamWriter(fs,System.Text.Encoding.GetEncoding("Gb2312"));
sr.WriteLine(content);
sr.Close();
sr = null;

Response.Write("<hr>生成完毕。<a href='" + FileNameUrl + "' target='_blank'>打开</a> <a href='" + Request.Url + "'>再来一个</a>");
//Response.Redirect(FileNameUrl);
Response.End();


}
</script>
<body>
<form runat=server>


<a href="a.aspx?id=" onclick="this.href=this.href+TextContent.value">ok</a>
<asp:TextBox id="TextContent" runat="server" TextMode = "MultiLine" Width="100%" Height="300" Text="<b>要请客了哈,干起来也有精神啊!</b>"/>
<asp:button id="ok" Text = "生成文件" runat="server" OnClick="genFile"/>
</form>
</body>
</HTML>