Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6116043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:09:39+00:00 2026-05-23T15:09:39+00:00

I’ve developed custom HTTP module that processes .aspx page (it just sets app.Response.Filter for

  • 0

I’ve developed custom HTTP module that processes .aspx page (it just sets app.Response.Filter for doing some simple string replacing) after it is rendered by ASP.NET. It is working perfectly, but I am running into one small problem – OutputCache HTTP module will not cache changes I’m doing with app.Response.Filter.

Because of performance benefit I would love if it would be somehow possible to inverse String Replacing and Output Caching.

So, is there a way to do this? Would using HttpHandlers be the way to go?

Here is the current source code of replacer:

public class StringReplaceModule : IHttpModule
{
    void IHttpModule.Dispose()
    {
        // Nothing to dispose; 
    }

    void IHttpModule.Init(HttpApplication context)
    {
        context.PreSendRequestHeaders +=
          (sender, e) => HttpContext.Current.Response.Headers.Remove("Server");

        context.BeginRequest += new EventHandler(context_BeginRequest);
    }

    void context_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication app = sender as HttpApplication;
        string url = app.Request.RawUrl.ToLower();
        if (!url.Contains(".aspx/") &&
            (url.Contains(".aspx") || url.Contains(".css") || url.Contains("/shorturl/")))
        {
            app.Response.Filter = new StringReplaceFilter(app.Response.Filter);
        }
    }

    #region Stream filter

    private class StringReplaceFilter : Stream
    {
        public StringReplaceFilter(Stream sink)
        {
            _sink = sink;
        }

        private Stream _sink;
        private static string[] find;
        private static string[] replace;
        static StringReplaceFilter()
        {
            var config = StringReplaceModuleConfig.CurrentConfigSection();

            find = config.Find.ToArray();
            replace = config.Replace.ToArray();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            byte[] data = new byte[count];
            Buffer.BlockCopy(buffer, offset, data, 0, count);
            string html = System.Text.Encoding.Default.GetString(buffer);

            for (int i = 0; i < find.Length; i++)
            {
                html = html.Replace(find[i], replace[i]);
            }

            byte[] outdata = System.Text.Encoding.Default.GetBytes(html);
            _sink.Write(outdata, 0, outdata.GetLength(0));
        }


        #region Less Important

        public override bool CanRead
        {
            get { return true; }
        }

        public override bool CanSeek
        {
            get { return true; }
        }

        public override bool CanWrite
        {
            get { return true; }
        }

        public override void Flush()
        {
            _sink.Flush();
        }

        public override long Length
        {
            get { return 0; }
        }

        private long _position;
        public override long Position
        {
            get { return _position; }
            set { _position = value; }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return _sink.Read(buffer, offset, count);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return _sink.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            _sink.SetLength(value);
        }

        public override void Close()
        {
            _sink.Close();
        }

        #endregion
    }
    #endregion
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:09:39+00:00Added an answer on May 23, 2026 at 3:09 pm

    Can you paste some sample code of what you are doing here?

    It sounds like you are trying to find a way of replacing any instance of a certain word/keyword with something else if you find it in the output?

    You can try doing this http://forums.asp.net/t/1123505.aspx and adding a explicit expiration rather than using the output cache facilities of asp.net

     using System;
     using System.Web;
    
     namespace TT.Web.HttpModules
     {
          /// <summary>
          /// HttpModule to prevent caching 
          /// </summary>
          public class NoCacheModule : IHttpModule
          {
             public NoCacheModule()
             {
             }
    
             #region IHttpModule Members
    
             public void Init(HttpApplication context)
             {
                 context.EndRequest += (new EventHandler(this.Application_EndRequest));
             }
    
             public void Dispose()
             {
             }
    
             private void Application_EndRequest(Object source, EventArgs e) 
             {
                 HttpApplication application = (HttpApplication)source;
                 HttpContext context = application.Context;
                 context.Response.Cache.SetLastModified(DateTime.Now);
                 context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(GetExpiryTime()));
                 context.Response.Cache.SetCacheability(HttpCacheability.Public);
                 context.Response.Cache.AppendCacheExtension("post-check=7200");
             //The pre-check is in seconds and the value configured in web.config is in minutes. So need to multiply it with 60
                 context.Response.Cache.AppendCacheExtension("pre-check=" + (GetExpiryTime() * 60).ToString());
                 context.Response.CacheControl = "public";
             }
    
             #endregion
         }
     }
    
    • Also – Is this page being loaded only once? Or on a postback?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.