是否可以读取 aspx
文件并呈现为 html
文件,并将生成的 html
文件写入磁盘?
.aspx
文件在没有代码隐藏文件的文件系统上。如果可能,请提供一些示例代码。
最佳答案
来自远程 url
byte[] buf = new byte[8192];
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(path);
webRequest.KeepAlive = false;
string content = string.Empty;
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
if (!(webResponse.StatusCode == HttpStatusCode.OK))
if (_log.IsErrorEnabled) _log.Error(string.Format("Url {0} not found", path));
Stream resStream = webResponse.GetResponseStream();
int count = 0;
do
{
count = resStream.Read(buf, 0, buf.Length);
if (count != 0)
{
content += encoding.GetString(buf, 0, count);
}
}
while (count > 0);
来自网络或虚拟路径
string content = string.Empty;
path = HttpContext.Current.Server.MapPath(path);
if (!File.Exists(path))
if (_log.IsErrorEnabled) _log.Error(string.Format("file {0} not found", path));
StreamReader sr = new StreamReader(path, encoding);
content = sr.ReadToEnd();
关于c# - 从文件系统读取 ASPX 文件并呈现为 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3149270/