如何在输出之前立即修改整个ASP.NET页面内容? [英] How can I modify the entire ASP.NET page content right before it's output?

查看:47
本文介绍了如何在输出之前立即修改整个ASP.NET页面内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有很多用户控件.我希望能够在代码中直接替换的内容中直接包含宏"或占位符".没关系,但是我将Ektron用作CMS.

I have a page that has a bunch of user controls on it. I want to be able to have "macros" or "placeholders" directly in the content that will get replaced in my code. It shouldn't really matter, but I'm using Ektron as my CMS.

在发送给客户端之前,是否可以挂入任何页面事件以对整个呈现的页面内容进行字符串替换?

Are there any page events that I can hook into to do a string replace on the entire rendered page content, right before it's sent to the client?

更新

这是我目前用于完成此操作的代码:

Here is the code that I am currently using to accomplish this:

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);
}

推荐答案

您是否尝试覆盖render方法?

Have you tried overriding the render method?

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()););
}

这篇关于如何在输出之前立即修改整个ASP.NET页面内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆