c# - 从文件系统读取 ASPX 文件并呈现为 HTML

标签 c# asp.net rendering

是否可以读取 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/

相关文章:

c# - 如何调试Web Service?

3d - 在工程应用程序中进行实时 3D 渲染的最佳方法是什么?

CSS 动画缩放变换在 Firefox 中开始模糊

angularjs - Freemarker 与 AngularJS

c# - 如何生成基本表单来快速编辑数据库条目?

c# - 将值从 aspx 传递到代码隐藏中的事件处理程序

c# - 弹出窗口替代 Windows Phone 8.0

c# - 逐字字符串文字 v 转义序列

javascript 与原型(prototype) js 冲突

asp.net - 禁用 ASP.NET HttpHandler 响应缓存