Accessing the BeginRequest event

Last post 11-12-2007 12:16 PM by foreachbiscuit. 1 replies.

Sort Posts:

  • Accessing the BeginRequest event

    11-09-2007, 4:46 PM
    • Participant
      1,226 point Participant
    • John Hoge
    • Member since 11-03-2004, 11:27 AM
    • Posts 341

    Hi,

     I would like to handle the BeginRequest event for requests to a specific directory. I know I can handle this in the Global.asax file, with something like this:

     if (<request is in directory I care about>) 

        // run this code

    What's the best way to have code that runs whenever a user hits a specific directory, but before the page is loaded?




     

  • Re: Accessing the BeginRequest event

    11-12-2007, 12:16 PM
    Answer

     You could implement a custom HttpModule and provide an event handler for the application's begin request:
    (I'm assuming you want an advanced use of code as you posted in HttpModules/HttpHandlers forum)

    public class TestModule: IHttpModule
    {
    
    	public void Init(HttpApplication application) 
    	{ 
    		application.BeginRequest += new EventHandler(this.Application_BeginRequest); 
    	} 
    
    	private void Application_BeginRequest(object sender, EventArgs e) 
    	{ 
    		HttpApplication application = (HttpApplication)sender; 
    		HttpRequest request = application.Context.Request;
    
    		// Use the Request properties to identify directory
    		if( is in directory I care about> )
    				DoSomething();
    	}
    }
    
     
     
    Regards,
    foreachbiscuit
    blog @ http://foreachbiscuit.wordpress.com
    Filed under:
Page 1 of 1 (2 items)