Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. how to capture content markup within an httpmodule? [modified]

how to capture content markup within an httpmodule? [modified]

Scheduled Pinned Locked Moved ASP.NET
helpjavascripthtmlcssregex
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jszpila
    wrote on last edited by
    #1

    Hello all, I currently have an httpmodule that extracts various information regarding the response that is being sent to the client. One thing I want to implement is a count of how many external files (.js or .css) are being included in the page. I have a regex function for this, but the problem I'm having is I can't quite figure out how to access the markup that is being sent to the client. Here's most of what I have so far:

    public class ResponseSizeHttpModule : IHttpModule
    {

        public void Init(HttpApplication application)
        {
            if (Convert.ToBoolean(ConfigurationManager.AppSettings\["CheckResponseSize"\]) == true)
            {
                application.BeginRequest += new EventHandler(this.AddFilter);
                application.EndRequest += new EventHandler(this.ResponseMessage);
            }
        }
    
        private void AddFilter(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            application.Response.Filter = new CounterClass(application.Response.Filter);
        }
        
        private void ResponseMessage(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            long lPageSize = application.Response.Filter.Length;
            long lRespLimit = Convert.ToInt64(ConfigurationManager.AppSettings\["ResponseLimitBytes"\]);
    
            if (lPageSize > lRespLimit)
            {
                **string strResponse = ""; //get the content of the response here!**
                int intNumIncludes = CountIncludes(strResponse);
                //StringBuilder sbAlertMsg = new StringBuilder();
                //build message here
                //HttpContext.Current.Response.Write(sbAlertMsg);
            }
        }
    
        private int CountIncludes(string strResponse)
        {
            Regex reScripts = new Regex(".js|.css", RegexOptions.IgnoreCase);
    
            return reScripts.Matches(strResponse).Count;
        }
    
        public void Dispose(){}
    }
    

    The bolded part is where I'd like to try to do this. I'm pretty new to httpmodules and the like so any suggestions on more effective methods are welcome, as would any other help. Thanks in advance!

    ------------------- abort, retry, fail?

    M 1 Reply Last reply
    0
    • J jszpila

      Hello all, I currently have an httpmodule that extracts various information regarding the response that is being sent to the client. One thing I want to implement is a count of how many external files (.js or .css) are being included in the page. I have a regex function for this, but the problem I'm having is I can't quite figure out how to access the markup that is being sent to the client. Here's most of what I have so far:

      public class ResponseSizeHttpModule : IHttpModule
      {

          public void Init(HttpApplication application)
          {
              if (Convert.ToBoolean(ConfigurationManager.AppSettings\["CheckResponseSize"\]) == true)
              {
                  application.BeginRequest += new EventHandler(this.AddFilter);
                  application.EndRequest += new EventHandler(this.ResponseMessage);
              }
          }
      
          private void AddFilter(Object source, EventArgs e)
          {
              HttpApplication application = (HttpApplication)source;
              application.Response.Filter = new CounterClass(application.Response.Filter);
          }
          
          private void ResponseMessage(Object source, EventArgs e)
          {
              HttpApplication application = (HttpApplication)source;
              long lPageSize = application.Response.Filter.Length;
              long lRespLimit = Convert.ToInt64(ConfigurationManager.AppSettings\["ResponseLimitBytes"\]);
      
              if (lPageSize > lRespLimit)
              {
                  **string strResponse = ""; //get the content of the response here!**
                  int intNumIncludes = CountIncludes(strResponse);
                  //StringBuilder sbAlertMsg = new StringBuilder();
                  //build message here
                  //HttpContext.Current.Response.Write(sbAlertMsg);
              }
          }
      
          private int CountIncludes(string strResponse)
          {
              Regex reScripts = new Regex(".js|.css", RegexOptions.IgnoreCase);
      
              return reScripts.Matches(strResponse).Count;
          }
      
          public void Dispose(){}
      }
      

      The bolded part is where I'd like to try to do this. I'm pretty new to httpmodules and the like so any suggestions on more effective methods are welcome, as would any other help. Thanks in advance!

      ------------------- abort, retry, fail?

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      There's a technique used in this article[^] that is similar to what you're describing. See if that is useful to you.

      J 1 Reply Last reply
      0
      • M Mike Ellison

        There's a technique used in this article[^] that is similar to what you're describing. See if that is useful to you.

        J Offline
        J Offline
        jszpila
        wrote on last edited by
        #3

        That did get me closer; I now have the functionality I need but it required another filter and module. Now I just need to get them to play nice together. Thanks!

        ------------------- abort, retry, fail?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups