我有一个页面,上面有一堆用户控件。我希望能够在将在我的代码中替换的内容中直接包含“宏”或“占位符”。这应该没什么关系,但我正在使用 Ektron 作为我的 CMS。
在将整个呈现的页面内容发送给客户端之前,是否有任何我可以 Hook 的页面事件来对整个呈现的页面内容进行字符串替换?
更新
这是我目前用来完成此任务的代码:
protected override void Render(HtmlTextWriter writer)
{
string content = string.Empty;
using (var stringWriter = new StringWriter())
using (var htmlWriter = new HtmlTextWriter(stringWriter))
{
// render the current page content to our temp writer
base.Render(htmlWriter);
htmlWriter.Close();
// get the content
content = stringWriter.ToString();
}
// replace our placeholders
string newContent = content.Replace("$placeholder1$", "placeholder1 data").Replace("$placeholder2$", "placeholder2 data");
// write the new html to the page
writer.Write(newContent);
}
最佳答案
您是否尝试过覆盖渲染方法?
protected override void Render(HtmlTextWriter writer)
{
StringBuilder htmlString = new StringBuilder(); // this will hold the string
StringWriter stringWriter = new StringWriter(htmlString);
HtmlTextWriter tmpWriter = new HtmlTextWriter(stringWriter);
Page.Render(tmpWriter);
writer.Flush();
writer.Write(DoReplaceLogic(htmlString.ToString()););
}
关于c# - 如何在输出之前修改整个 ASP.NET 页面内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1580022/