我怎样才能修改整个ASP.NET页面内容的权利之前,它的输出? [英] How can I modify the entire ASP.NET page content right before it's output?

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

问题描述

我有了一堆上用户控件的页面。我希望能够有宏,或直接将在我的code被替换的内容占位符。它不应该真正的问题,但我使用晔作为我的CMS。

有没有我可以挂接到做一个字符串对整个呈现的页面内容替换,右,然后再将其发送到客户端的页面事件?

更新

下面是code,我目前正在使用来实现:

 保护覆盖无效渲染(HtmlTextWriter的作家)
{
    字符串内容=的String.Empty;    使用(VAR的StringWriter =新的StringWriter())
    使用(VAR的HTMLWriter =新的HtmlTextWriter(StringWriter的))
    {
        //渲染当前页面内容,我们的临时作家
        base.Render(的HTMLWriter);
        htmlWriter.Close();        //获取内容
        含量= stringWriter.ToString();
    }    //代替我们的占位符
    。字符串newContent = content.Replace($ PLACEHOLDER1 $,PLACEHOLDER1数据)更换($ PLACEHOLDER2 $,PLACEHOLDER2数据);    //写入新的HTML页面
    writer.Write(newContent);
}


解决方案

您是否尝试过重写render方法?

 保护覆盖无效渲染(HtmlTextWriter的作家)
{
   StringBuilder的htmlString =新的StringBuilder(); //这将持有该字符串
   StringWriter的StringWriter的=新的StringWriter(htmlString);
   HtmlTextWriter的tmpWriter =新的HtmlTextWriter(StringWriter的);
   Page.Render(tmpWriter);
   writer.Flush();   writer.Write(DoReplaceLogic(htmlString.ToString()););
}

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?

UPDATE

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

解决方案

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天全站免登陆