c# – 如何在输出之前修改整个ASP.NET页面内容?

前端之家收集整理的这篇文章主要介绍了c# – 如何在输出之前修改整个ASP.NET页面内容?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个页面上有一堆用户控件.我想能够直接在我的代码中被替换的内容中有“宏”或“占位符”.这不是很重要,但我使用Ektron作为我的CMS.

在发送到客户端之前,是否有任何页面事件可以挂接到整个呈现页面内容上的字符串替换?

UPDATE

这是我目前用来完成这个的代码

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#相关文章