locked
Http Module Application_BeginRequest Executing too many times RRS feed

  • Question

  • User162601688 posted

    I've implemented the following Http Module which on my homepage executes 20 times!

    I left out the commented code -- I was trying to debug if something was causing this, I set my break point on the "string fullOrigionalpath" line below, which executes numerous times.

    I tried set my start page to another "test" page which has only a few controls, but it executes more than 10 times.

    I though the code I put in an Http Module was only supposed to execute once per page?

    Thank you.

     

    public class URLListener : IHttpModule

    {

    public URLListener() { }public void Init(HttpApplication context)

    {

    context.BeginRequest +=
    new EventHandler(Application_BeginRequest);

    }

    private void Application_BeginRequest(object source, EventArgs e)

    {

    HttpContext context = ((HttpApplication)source).Context;

    //Breakpoint here.

    string fullOrigionalpath = HttpContext.Current.Request.Url.ToString();

    }

    }

    Monday, April 6, 2009 2:34 PM

Answers

  • User-590788913 posted

    The dev box/cassini server always handles all file types, so you cannot escape that if you are going to continue using this method to develop your application. The difference by using IIS is that IIS bypasses the static files (JS, CSS, images, flash ...) so they won't be handled by ASP.NET engine. Hence, save some unnecessary process time.

    In your dev setup, you can apply the filter before any further logic to run based on the file type, such as the following example:

     

    if (HttpContext.Current.Request.Path.ToLowerInvariant().IndexOf(".aspx") > -1)
    {
    return; // bypass all other processing in BeginRequest handle
    }

    // code to handle URL redirects below ...
      

    Your redirect table should work fine, this has nothing to do with the multiple requests involved within the BeginRequest handle. 

    On a side note, you need to differentiate the URL redirection code 301 for permanent and 302 for temporary, more details explained here. It's important to let search engine index your pages correctly.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 6, 2009 9:49 PM

All replies

  • User-590788913 posted

    What's your test environment when you get 20 times? If it's Cassini server, a.k.a. the dev web server, it's normal as every single web request are routed as an ASP.NET request, which includes all the static CSS, JS and images files etc., each file will cause a request. Once you publish the site to the IIS, you will expect number of requests drop as those static file types are bypassed in default IIS setting.

    HttpModule is not meant to be executed per page basis, it's a pre-process of any page request, if your ASPX page also contains a HttpHandler call, it will be 2 requests processed by the HttpModule. Here's detailed info on the concept of HttpModule and HttpHandler.

    Monday, April 6, 2009 4:32 PM
  • User162601688 posted

    No, it's my dev box VS 2005.

    Wow!  I guess I've been mislead that that is the way to go.

    If I'm understanding you correctly, HttpModules are called for every JS/CSS file in each page, at least on my dev box, then is there a way to prevent it from occuring on my dev box?
    Currently, the site uses the VS-integrated web server, not IIS on my dev box -- is there a difference as it related to this issue?

    I'm trying to write a "URL Listener/redirector" the following way.  I would appreciate any advice you may have.

    I have a db table of "old urls" and "new urls" that will be updated periodically.
    At the beginning of the application, I want to load that list (old/new urls) into a hashtable, then memory.
    On each page request, I want to intercept the URL, search through the list looking for a match between the current URL and "old urls."
    If there's a match, I'll simply redirect (or Context.Rewrite() ) using the "new url."

    Don't worry, I'm not asking for a bunch of code for menial tasks.
    If would help if you could point me in the right direction of how to do these steps (what ASP.NET feature to execute the URL-intercept, once per page, etc.

    Thank you.
    Monday, April 6, 2009 8:05 PM
  • User-590788913 posted

    The dev box/cassini server always handles all file types, so you cannot escape that if you are going to continue using this method to develop your application. The difference by using IIS is that IIS bypasses the static files (JS, CSS, images, flash ...) so they won't be handled by ASP.NET engine. Hence, save some unnecessary process time.

    In your dev setup, you can apply the filter before any further logic to run based on the file type, such as the following example:

     

    if (HttpContext.Current.Request.Path.ToLowerInvariant().IndexOf(".aspx") > -1)
    {
    return; // bypass all other processing in BeginRequest handle
    }

    // code to handle URL redirects below ...
      

    Your redirect table should work fine, this has nothing to do with the multiple requests involved within the BeginRequest handle. 

    On a side note, you need to differentiate the URL redirection code 301 for permanent and 302 for temporary, more details explained here. It's important to let search engine index your pages correctly.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 6, 2009 9:49 PM