asp.net生成静态页的方法
1、直接将页面内容存在变量中后输出:
StringBuilder IndexContentResult= new StringBuilder(); //存放输出页面的HTML
IndexContentResult.Append("<html>\n");
IndexContentResult.Append(" <head>\n");
IndexContentResult.Append(" <title>title</title> \n");
IndexContentResult.Append(" </head>\n");
IndexContentResult.Append(" <body>\n");
...
IndexContentResult.Append(" <body>\n");
IndexContentResult.Append("</html>\n");
string tempfile = Server.MapPath("~");
tempfile = tempfile + "index.htm";
System.IO.StreamWriter sr = new System.IO.StreamWriter(tempfile, false, System.Text.Encoding.Default);
sr.Write(IndexContentResult.ToString());
sr.Close();
2、用模板替换:
template.htm //模板文件
<html>
<head>
<title>$title$</title>
</head>
<body>
$body$
</body>
</html>
.cs代码文件
string title = "生成的网页标题";
string body = "生成的网页内容";
string filename = Server.MapPath("~/") + "frame_a/index.htm";
System.IO.StreamReader srm = new System.IO.StreamReader(filename,System.Text.Encoding.Default);
string mb = srm.ReadToEnd();
srm.Close();
string tempfile = Server.MapPath("~/") + "index.htm";
StreamWriter sr = new System.IO.StreamWriter(tempfile, false, System.Text.Encoding.Default);
mb = mb.Replace("$title$", title);
mb = mb.Replace("$body$", body);
sr.Write(mb);
sr.Close();
相关文档:
asp.net 发布网站时有三个选项:
1、允许更新此预编译站点:asp.net web 页面通常包含两个页面,一个即 .aspx 页,还有一个 .aspx.cs 文件,后一个文件是基本委托的事件响应文件代码;此处允许更新此预编译站点的意思就是:.aspx 页可根据需要进行一定的更新,而 .aspx.cs 编译成的 .dll 保持不变;另外,如果发布网站时只 ......
一、使用QueryString参数
QueryString将传递的值显示在浏览器的地址栏中,是一种非常简单也使用比较多的传值方式。
如果传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法
/// <summary>
/// 使用QueryString变量
/// </summary>
/// <param name="param1"></param>
/// ......
HttpContext.Current.Request.Url.ToString() 并不可靠。
如果当前URL为
http://localhost/search.aspx?user=http://csharp.xdowns.com&tag=%BC%BC%CA%F5
通过HttpContext.Current.Request.Url.ToString()获取到的却是
http://localhost/search.aspxuser=http://csharp.xdowns.com&tag=¼&fra ......
+++ 修改WebConfig文件:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="connStr" value="Data Source=ora11g;uid=scott;pwd=tiger;unicode=true"/>
</appSettings>
<connectionStrings>
<ad ......
您可以使用HTTP模块,一个到ASP.NET HttpApplicationState类的扩展,在Global.asax编写代码强制ASP.NET在每一个页面请求时自动注入依赖的对象,就像在ASP.NET Web窗体应用程序中讨论的一样.
下列方法显示了一个合适的方法能够获取PreRequestHandlerExecute事件将它自己注入到ASP.NE ......